for(it = gameObjects.begin();it!=gameObjects.end();it++){
it->second->update(frameTime);
if(it->second->getSprite()->GetPosition().y > 500){
std::cout << "Removing enemy" << std::endl;
std::map<sf::String,VisibleGameObject*>::iterator itor = Remove(it->second->getName());
if(itor!=gameObjects.end()){
std::cout << "itor doesn't equal" << std::endl;
it=itor;
}else{
std::cout << "itor = end" << std::endl;
it=itor;
}
}
}
As soon as itor = end is printed, it errors – “map set iterator not incrementable”. I thought the for loop should end before it increments again, as it!=gameObjects.end() will be false after this. Adding a break in the else statement resolves the problem.
Why doesn’t it work without the break? I’m assuming it’s something to do with when the iterator is incremented compared to when the condition is checked.
You assume correct. The iterator is incremented at the end of the loop, and then the condition is checked.
So after “itor = end” is printed, it gets incremented to
gameObjects.end()++which of course is not valid. You could get around by checking foritor == gameObjects.end()inside the loop, and then breaking.EDIT: As pointed out in the comments, you’re better just removing the ++it from the loop, to avoid skipping over the element after a removed element. For example: