auto iterators and vector.erase()?

Hi, I have a problem when trying to erase an element in a vector when using an auto iterator:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
for (auto &enemyItr : mEnemies)
{
    for (auto &projItr : mProjectiles)
    {
	// We only want to check against ROCKETS here
	if (projItr->Type() != Projectiles::PROJ_ROCKET) continue;

	distX = projItr->Position().X - enemyItr->Position().X;
	distY = projItr->Position().Y - enemyItr->Position().Y;

	squareDist = distX * distX + distY * distY;

	obj1Radius = projItr->Radius();
	obj2Radius = enemyItr->Radius();

        if (squareDist <= (obj1Radius + obj1Radius) * (obj2Radius + obj2Radius))
	{
	    projItr.reset();
	    enemyItr.reset();
	    mProjectiles.erase(projItr);
	    mEnemies.erase(enemyItr);

	    mScore++;
	    goto ROCKETS_ENEMIES_STOP;
	}
    }
}


For some reason the lines:

1
2
mProjectiles.erase(projItr);
mEnemies.erase(enemyItr);


gives me an error that "no instance of the overloaded method matches the argument list". If I use a normal iterator (without auto) it works. Something I'm missing?

Edit: mEnemies is a "std::vector<shared_ptr<Enemy>>" and mProjectiles is a "std::vector<unique_ptr<Projectile>>".

Best Regards,
Robin
Last edited on
Neither enemyItr nor projItr are actually iterators.
Okay...?
In a range-based for loop, the type of enemyItr is (a reference to) the same type as the elements contained in mEnemies. It is not an iterator. The same for projIter.

This should be very obvious from the compiler errors generated (or not generated) in the code previous to that referenced in your OP. For instance, iterators have no reset member.
Last edited on
Topic archived. No new replies allowed.