Trying to make a game with C++, using lists to store bullets and enemies, but they are not erased
- by XD_dued
I've been trying to make a pretty simple space shooter game with C++, and I have been having a lot of trouble trying to use lists to store enemies and bullets.
Basically I followed the post here almost exactly to store my bullets:
SDL Bullet Movement
I've done something similar to store my enemies.
However, when I call bullets.erase(it++), for some reason the bullet is not erased.
When the bullet movement is run for the next frame, it tries to re delete the bullet and segfaults the program. When I comment out the erase line, it runs fine, but the bullets are then never erased from the list...
Is there any reason why the elements of the list aren't being deleted? I also set it up to print the number of elements in the list for every iteration, and it does not go down after deleting.
Thanks!
EDIT:
Here's the specific code I'm using to store my enemies and having them act:
std::list<Grunt*> doGrunts(std::list<Grunt*> grunts)
{
for(std::list<Grunt*>::iterator it = grunts.begin(); it != grunts.end();)
{
if((*it)->getHull() == 0)
{
delete * it;
grunts.erase(it++);
}
else
{
(**it).doUnit(grunts, it);
++it;
}
}
}
Grunt is my enemy class, and the list grunts is a global variable. Is that my problem? When I pass the global into the function it becomes local? I assumed lists would be a reference type so thought this wouldn't be a problem.
Sorry if this was a dumb question, I'm very new to C++, this is the first major thing I'm working on.