简体   繁体   中英

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(); .

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM