使用 C++ 将我的游戏引擎中的一些代码从 Mac 移植到 Windows,我得到这个运行时错误:“矢量擦除超出范围”。它适用于 Mac!
void Entity::runDeferreds() {
for (auto it = deferreds.begin(); it != deferreds.end(); /* nothing */ ) {
if (it->Condition()) {
it->Execution();
it = deferreds.erase(it);
} else {
++it;
}
}
}
这会遍历一个“延迟”任务列表,这些任务存储在一个std::vector<DeferredCall>
被调用的deferreds
. 如果DeferredCall
'sCondition()
得到满足,那么它Execution()
就会运行,并且应该从vector
. 但是,相反,我得到了上述错误!
DeferredCall 看起来是这样的,并不是说它太重要了:
struct DeferredCall {
std::function<bool()> Condition;
std::function<void()> Execution;
};
帮助?!
编辑:- 替代方法
我也试过这个,再次在 Mac 上工作:
deferreds.erase(std::remove_if(deferreds.begin(), deferreds.end(),
[](DeferredCall &call) {
if (call.Condition()) {
call.Execution();
return true;
}
return false;
}
), deferreds.end());
但是,在这种情况下,我得到“向量迭代器不兼容”。