I'm currently developing a game using SDL2 along with C++.
My character shoots bullets and of course when one of them hits an enemy it has to be deleted from the std::vector he is in.
My vector of bullets is declared as follows
std::vector<Bullet> _bullets;
and when I try to delete one from it I do the following
void Player::DestroyBullet(Bullet b)
{
for (auto it = _bullets.begin(); it != _bullets.end();)
{
if ((*it).GetId() == b.GetId())
{
it = _bullets.erase(it);
}
else
{
++it;
}
}
}
The problem now is that while erasing that bullet, the compiler gives me an assertion fail which says "Expression: vector iterator not incrementable".
I searched some different solutions to delete an item from a vector (using iterators, using pop_back mixed with some other functions) but none of them worked. This vector is of course used by other functions in the game loop (drawing function which continuously iterates through it to draw the bullets textures and update function which takes care of all the logic).
May these functions raise this problem? It would be strange, because they use (of course) different iterators for their loops, so they should not care about this.
Thanks in advance for the help, hope you all have a good day!
Cheers!
P.S. I am programming with Visual Studio 2015.
Aucun commentaire:
Enregistrer un commentaire