我在我的C++程序执行过程中出现错误(向量迭代器不兼容),我不明白。 [(在Windows/VISUAL C++ 2008速成)std ::向量迭代器不相容
这里是我的问题的一个简化版本:
#include <vector>
class A
{
int mySuperInt;
public:
A(int val) : mySuperInt(val) {}
};
class B
{
std::vector<A*> myAs;
public:
B()
{
myAs.push_back(new A(1));
};
const std::vector<A*> getA() const {return myAs;}
};
int main()
{
std::vector<B>* myBs = new std::vector<B>;
myBs->push_back(B());
std::vector<B>::const_iterator it_B = myBs->begin();
for (; it_B != myBs->end(); ++it_B)
{
std::vector<A*>::const_iterator it_A = it_B->getA().begin();
for (; it_A != it_B->getA().end(); ++it_A) // <-- Error during execution: vector iterator incompatibles
{
// Do stuff
// ...
}
}
}
难道我错过了什么?
在此先感谢您的答案。
你得到一个运行时错误?我只是编译这个并在Mac OS X上用gcc运行它。 – chrisaycock 2010-10-22 04:18:06
那么你的问题是什么?你没有告诉我们你的问题是什么 - 难以回答... – 2010-10-22 04:26:47
“我的C++程序执行过程中出现错误(向量迭代器不兼容),我不明白”不够清楚吗?真的吗? – 2010-10-22 15:38:01