我有 2 个类,即 classA 和 classB。在 classA 中,有一个在堆内存上动态声明的映射。 然而,在 classB 中,正在尝试使用迭代器访问 classA 映射值。 不幸的是,我得到了迭代器不匹配运算符=的错误。 如果我将 map 移动到 classB,迭代器将正常工作。有人可以帮助我吗,这困扰了我一段时间。 提前致谢。
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 = object->_themap.begin(); //not _themap->begin()
因为 _themap
是一个非指针,所以你必须使用 .
运算符,而不是 ->
运算符。
此外,还有一些错误。如果您将 classA
编写为
//incorrect
classA{
//...
};
应该是
//correct
class classA{
//...
};
也就是说,您必须在类名 之前使用关键字class
。因此,使用关键字 class
定义其他类,例如 classB
。
关于c++ - 迭代器不匹配运算符=错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/6744109/