Hi in my project that's roguelike RPG im using vector to keep polymorphic indicators to all monsters, parts of abstract class Mob. The problem is during fight - there's a function that return iterator selecting nob that we are attacking(i skipped for tests that in case here's no monster it returns begin of vector).
vector<Mob*>::iterator Hero::TryAttack(vector <Mob*> monsters)
int trying_x = pos_x, trying_y = pos_y;
case UP: trying_y--;
case DOWN: trying_y++;
case LEFT: trying_x--;
case RIGHT: trying_x++;
for (vector <Mob*>::iterator i = monsters.begin(); i != monsters.end(); i++)
if ((*i)->pos_x == trying_x && (*i)->pos_y == trying_y)
and when i calling that function here:
there's poping assertion failure:
i understand that erase returns next element after deleted but how can i delete that only one existing element in vector?
Hero::TryAttack(vector <Mob*> monsters)
You are passing
monsters by value. Returned iterator refers to nonexistent copy of the vector. Pass it by reference:
Hero::TryAttack(vector <Mob*>& monsters)