Vector - Erase object from vector


How does one erase a Game Object from the vector . Anyone spot what Im doing wrong

	if (!projectiles.empty())
		for (auto vehicle : this->vehicles)
			for (auto projectile : this->projectiles)

				if (projectile->getSprite() != NULL)
					cocos2d::Rect rect1 = vehicle->getSprite()->getBoundingBox();
					cocos2d::Rect rect2 = projectile->getSprite()->getBoundingBox();

					if (rect1.intersectsRect(rect2))
						cocos2d::Vec2 location = projectile->getSprite()->getPosition();
						projectiles.erase(projectile);//This DOT is highlighted red and wont work
						Explosion* explosion = new Explosion();


Objects Inherit like this and my vector is

GameObject -> GameObjectPhysics - > Bullet

std::vector<GameObject*> projectiles;

Thanks Very Much any help


I use std::remove`

#include <algorithm>
vec.erase(std::remove(vec.begin(), vec.end(), 8), vec.end());


Not sure what this does, also doesn’t work.

Is there a way at just deleting the current iter (projectile)?


Maybe use Vector instead and then you can give an index or pointer.
If you’d stick with vector, I think it should be like this:

int index = 5;
vec.erase(vec.begin() + index);


How do I know which index the object is on ? Some kind of increment int during looping the vector ?


Ah, so you don’t know the index. Then eraseObject from Vector class is the easiest option.
In vector you can find the iterator:

auto it = find(vec.begin(), vec.end(), itemToFind);
if (it != vec.end()){