Pure virtual function called at runtime.

I'm currently making a game and what happens is that during runtime, it suddenly closes and a message is shown in the console saying "Pure virtual function called at runtime". I have no idea how to fix it and now I'm stuck. So any help would be greatly appreciated.

Here is the code: https://github.com/BlueFridge/sweet-run-2/blob/master/main.cpp
The problem seems to occur somewhere between lines 662 - 695. And it seems to only happen when the size of the vector reaches 1.

Thanks.
Last edited on
I think the problem is the way you are calling erase inside the loop.

After you have erased the item on line 691 you still try to display it on line 694 but itemVector[i] will then be the item after the erased item and there might not even be such an item. You probably should put 694 in an else clause.

Another problem is that you increment i on every iteration, even when an item is erased. That will make the loop skip the item after an erased item. I guess that is not what you want so instead doing ++i on line 662 you could move it to the end of the else clause that I suggested you added earlier so that it is only incremented if no item was erased.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
for(int i = 0; i < itemVector.size();)
{
	if(itemVector[i].checkCollision(Player))
	{
		...
		itemVector.erase(itemVector.begin()+i);
		scoreText.setString("Score: "+toStr(score));
	}
	else
	{
		itemVector[i].display(window);
		i++;
	}
}
Seems like that fixed it. Thanks for the help and explanation! :)
Topic archived. No new replies allowed.