运行时错误:映射/设置迭代器不兼容
我在第 8 行遇到运行时错误“映射/设置迭代器不兼容”。
void Manager::Simulate(Military* military, Shalishut* shalishut,char* args[]){
Simulation* simulation = Simulation::GetInstance();
Time* time = Time::GetInstance();
multimap<int,Task*>::iterator itTasks;
itTasks = simulation->GetTasks().begin();
while(itTasks != simulation->GetTasks().end()){
while (itTasks->second->GetTimeStamp() == time->GetTime()){ /*line 8 - ERROR*/
TaskExecute(itTasks->second,military,shalishut,args);
itTasks++;
}
// Unit take car of vehicles
time->TimeIncrease();
}
}
Simulation
被声明为 multimap
。问题是什么?
I have a runtime error "map/set iterators incompatible" at line 8.
void Manager::Simulate(Military* military, Shalishut* shalishut,char* args[]){
Simulation* simulation = Simulation::GetInstance();
Time* time = Time::GetInstance();
multimap<int,Task*>::iterator itTasks;
itTasks = simulation->GetTasks().begin();
while(itTasks != simulation->GetTasks().end()){
while (itTasks->second->GetTimeStamp() == time->GetTime()){ /*line 8 - ERROR*/
TaskExecute(itTasks->second,military,shalishut,args);
itTasks++;
}
// Unit take car of vehicles
time->TimeIncrease();
}
}
Simulation
is declared as a multimap<int,Task*>
. What is the problem?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
我将大胆猜测一下
Simulation::GetTasks()
签名如下所示:每次您创建一个新的多重映射(一个副本)调用它。
比较迭代器时,两个
multimap
迭代器必须来自同一个容器;由于每次调用GetTasks()
时都会获得一个新副本,因此违反了此约束,这就是错误的根源。您还有另一个问题 - 临时多重映射副本在创建它们的语句后被销毁,因此您的迭代器立即失效。你有两个选择;一种是在本地捕获副本并一致地使用该副本:
另一种是让
GetTasks()
返回对持久多重映射的引用,确保每次都使用相同的多重映射:或常量引用:
这具有避免复制
multimap
的(可能很大的)开销的优点。请注意,使用 const 引用需要使用 const_iterator 来逐步执行多重映射。我建议同时定义 const 和非常量访问器(C++ 将根据
Simulation
指针或引用是否为 const 选择正确的访问器),除非您想禁止直接修改底层multimap
完全,在这种情况下,您只能定义const
变体。I'm going to take a wild guess and say that the
Simulation::GetTasks()
signature looks like this:This creates a new multimap (a copy) each time you call it.
When comparing iterators, both of the
multimap<int,Task*>
iterators must come from the same container; since you're getting a new copy each time you callGetTasks()
, you violate this constraint, and this is the source of your error. You also have another problem - the temporary multimap copies are destroyed after the statement they're created in, so your iterators are invalidated instantly.You have two choices; one is to capture a copy locally and use that copy consistently:
Another is to have
GetTasks()
return a reference to a persistent multimap, ensuring the same one is used each time:Or a const reference:
This has the advantage of avoiding the (potentially large) overhead of copying the
multimap
.Note that using a const reference requires using
const_iterator
s to step through the multimap. I would recommend defining both const and non-const accessors (C++ will pick the right one based on if theSimulation
pointer or reference is const), unless you want to disallow direct modification of the underlyingmultimap
entirely, in which case you can define only theconst
variant.