Map/Set iterators not incrementable.

So I have the following piece of code:

1
2
3
4
5
6
7
8
9
10
11
12
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?

Thanks in advance, Xentro
Last edited on
It should work flawlessly.
What is the return type of GetGameObjects?

Is it compile-time or runtime error? What call stack and parameters is if it is runtime error? What is exact error message?
First of all, thanks for your reply.

The return type of GetGameObjects() is a map, defined like this:

std::map<std::string, VisibleGameObject*>& GetGameObjects();

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:

Debug Assertion Failed!

Program: C:\Windows\system32\MSVCP120D.dll
File: c:\program files (x86)\microsoft visual studio 12.0\vc\include\xtree
Line: 262

Expression: map/set iterator not incrementable

I don't know if this is enough info? Feel free to ask!
Is it complete code of your loop? Maybe try to iterate over constant elements ( for (const auto & e : /*...*/ ).

Usually this error happens if map is changed during iteration. Are you sure there nothing changing your map (another thread for example)?

Could be by any chance that you accidentally return a temporary in GetGameObjects?
Last edited on
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.
Btw, I tried using const as you suggested but I still got the same error.
Ok apparently, the previous iterator is not the problem, I'll post my full function here for clarification:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
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)
Thanks for the info, I changed my code to the following:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
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);
				std::cout << "Monster added" << std::endl;
			}				
		}
	}
	for (auto & i : Monsters)
	{
		std::cout << "Trying to remove monster..." << std::endl;
		Game::GetGameObjectManager().Remove(i);
		std::cout << "Monster removed" << std::endl;
	}
}


And my output is as follows in the console window:

Monster added
Trying to remove monster...
Monster removed.

So the problem must be that it can't move on after it removed the monster somehow...

The remove function does the following:

1
2
3
4
5
6
7
8
9
void GameObjectManager::Remove(std::string name)
{
	std::map<std::string, VisibleGameObject*>::iterator results = _gameObjects.find(name);
	if (results != _gameObjects.end())
	{
		delete results->second;
		_gameObjects.erase(results);
	}
}


And that works perfectly with this code (that does a similar thing for another type of object):

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
void Player::CheckKeys()
{
	std::vector<std::string> RedKeys;

	for (auto & e : Game::GetGameObjectManager().GetGameObjects())
	{
		if (e.second->GetObjectType() == Key)
		{
			if (GetBoundingRect().intersects(e.second->GetBoundingRect()))
			{
				if (e.first.find("Red") != std::string::npos)
				{
					SetRedKey(true);
					for (auto & d : Game::GetGameObjectManager().GetGameObjects())
					{
						if (d.first.find("RedBrick") != std::string::npos)
						{
							d.second->SetSolid(false);
							d.second->Load("images/redbrick1.png");
						}
					}
				}
				RedKeys.push_back(e.first);
			}
		}
	}
	for (auto & i : RedKeys)
	{
		Game::GetGameObjectManager().Remove(i);
	}
}
Do the loop terminates succesfully? (Inset debug output after line 21 in your CheckMonsters function).

Where do you call CheckMonsters function? Do that function store iterator to GameObjects map?
Ok you fixed it, it was indeed the call of my CheckMonsters function within a loop that iterated over my GameObjects, thanks for pointing this out!!!
Last edited on
Topic archived. No new replies allowed.