std::vector<std::string> Monsters;
for (auto & e : Game::GetGameObjectManager().GetGameObjects())
{
if (e.second->GetObjectType() == Monst)
{
if (e.second->GetHealth()<=0)
{
Monsters.push_back(e.first);
}
}
}
But whenever I try to debug I get the error: Map/Set iterators not incrementable. When I type slashes before the "Monsters.push_back" line it works perfectly, but whenever it tries to put an element in my "Monsters" vector it gives the error.
I find this strange since I have a similar piece of code somewhere else in my files and it works perfectly, does anybody see what I'm doing wrong?
containing object names and pointers to said objects. This should clarify my code, I'm trying to see if the type of the object is a monster, then if it has no health left I want to add it's name to the vector (to later delete it from gameobjects).
I'm not sure what you mean by compile-time/runtime error. But the error occurs whenever I enter my game and get a monster at or below 0 HP (when the if-condition kicks in).
As I said, when I run my program without the line in the if-statement it works fine.
I'm not very used to C++ terminology, I don't know what you mean by call stack and parameters and such. But I get this:
Well, the code I posted in the first message is taken from my program literally. So that's really all it does. After that it shouldn't use "e" anymore, and there's nothing else it does with it.
void Player::CheckMonsters()
{
std::vector<std::string> Monsters;
for (auto & e : Game::GetGameObjectManager().GetGameObjects())
{
if (e.second->GetObjectType() == Monst)
{
if (e.second->GetHealth()<=0)
{
Monsters.push_back(e.first);
}
}
}
if (Monsters.size()>0)
{
for (auto & i : Monsters)
{
Game::GetGameObjectManager().Remove(i);
}
}
}
So first I put all monsters with 0 or less health in the vector, then I delete them from my GameObjectManager's list of object. now if I disable the delete part, I don't get any errors at all so I'm guessing the error must be concerning iterator "i" and not "e". Do you see anything wrong with this code?
1) you do not need to check if Monsters.size()>0. If it is 0, then next loop will iterate 0 times.
2) There might be problem with Remove function. Does it store iterator somewhere? What are parameters for it? You might add debug output (something as simple as outputting numbers on screen after each line to catch moment when actual error happens)