繁体   English   中英

C ++,将迭代器用于指向的地图容器时出错。 映射/设置迭代器不可递增

[英]C++, error when using iterator for a map container that is pointed to. map/set iterator not incrementable

尝试迭代另一个对象指向的地图时出现此错误。 当我不使用指针时,它可以工作。 (遍历成员映射“件”)因此我想知道该怎么做,或者是否不可能像这样遍历整个映射?

Board * Board::ccBoard(){

Board * newBoard = new Board();
map<Vec2, Piece>::iterator it;
for (it = newBoard->pieces.begin(); it != newBoard->pieces.end(); ++it)
    newBoard->removePiece(it->first);
return newBoard;
}

提前致谢!

所述removePiece()函数删除该元素it指的是,无效it 然后尝试增加it导致断言失败。 map::erase()

对已删除元素的引用和迭代器无效。

我不确定for循环的意图是什么,看来它将有效地清空map在这种情况下,只需使用map::clear()

newBoard->pieces.clear();

要修复,摆脱++it的for循环和取代it->firstit++->first

(这将使迭代器增加,并使用副本调用Ease()。)

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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