c++ - "Iterator not incrementable" while deleting an item from std::vector -
i'm developing game using sdl2 along c++.
my character shoots bullets , of course when 1 of them hits enemy has deleted std::vector in.
my vector of bullets declared follows
std::vector<bullet> _bullets;
and when try delete 1 following
void player::destroybullet(bullet b) { (auto = _bullets.begin(); != _bullets.end();) { if ((*it).getid() == b.getid()) { = _bullets.erase(it); } else { ++it; } } }
the problem while erasing bullet, compiler gives me assertion fail says "expression: vector iterator not incrementable".
i searched different solutions delete item vector (using iterators, using pop_back mixed other functions) none of them worked. vector of course used other functions in game loop (drawing function continuously iterates through draw bullets textures , update function takes care of logic).
may these functions raise problem? strange, because use (of course) different iterators loops, should not care this.
p.s. programming visual studio 2015.
edit 1: application not multi-threaded.
you should use ::std::remove_if
instead:
_bullets.erase ( ::std::remove_if ( _bullets.begin() , _bullets.end() , [&b](bullet const & bullet) { return (bullet.getid() == b.getid()); } ) , _bullets.end() );
Comments
Post a Comment