0

In my program i have a for loop which updates all the bullets/shots in my game. It looks like this:

for (auto sprite : shots)
{
    sprite->update(shots);
}

In the update function from the class, i delete a shot from the shots list if it detects a collision, like this:

void Shot::terminate(std::vector<Shot*>& shots)
{
    shots.erase(shots.begin() + index /*index is the elements number in the vector "shots"*/);
}

This function "terminate" is called if the update() class function detects a collision. But when a object from the list is deleted while in the for loop; the program crashes "vector subscript out of range". The counter in the for loop should be substracted by 1 every time a object gets deleted but i dont know how to do so in a c++11 for loop. Is it possible to fix my problem?

Noah.Ehrnstrom
  • 172
  • 1
  • 12

0 Answers0