开发者

Iterator no match operator= Error

I have 2 classes namely classA and classB. Within classA there is a map dynamically declared on the heap memory. In classB however, is trying to access classA map values using an iterator. Unfortunately I have gotten the error no match operator= for the iterator. If i were to move the map to classB, the iterator will work fine. Could someone assist me in this, it has been bothering me for awhile. Thanks in advance.

class classA{
public:
  classA();
friend classB;
private:
  map <int,int>* _themap;
};

classA::classA(){
  _themap = new map<int,int>;
}

class classB{
private:
 classA* object = new开发者_高级运维 classA();
 void accessthemap();
};

void classB::accessthemap(){

 map<int,int>::iterator it;
 it = object->_themap->begin();
 it = object->_themap->find();
}


It should be

it = object->_themap.begin(); //not _themap->begin()

Because _themap is a non-pointer, so with it you've to use . operator, instead of -> operator.

Beside, there are few more errors. If you've written classA as

//incorrect
classA{
   //...
};

which should be

//correct
class classA{
   //...
};

That is, you've to use the keyword class before the class-name. So define other classes such as classB using the keyword class.


You can't define members inside the class definition, so this is wrong:

classB{
private:
 classA* object = new classA();
 void accessthemap();
};

Instead just use a normal object (not to mention fix your other syntax errors):

class classB {
private:
 classA object;
 void accessthemap();
};

No need for dynamic allocation here.

Then write object._themap.begin();.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜