The error in this code:
for ( it = list.begin(); it != list.end(); ++it ) {
if(it->checkHit(mouseCoords.x,mouseCoord…
list.erase(it);
}
is that you ignore the return value of erase(it). Learn to never do that! The call to erase() invalidates your iterator "it" and any further access using it may (and most likely will) cause a crash. What you should do is write
it = list.erase(it);
because erase() returns a valid iterator at the next item in the vector, which you can safely continue incrementing and dereferencing.
And since then your next ++it would then skip over the element right after the one erased, the better loop is
for ( it = list.begin(); it != list.end(); ) {
if(it->checkHit(mouseCoords.x,mouseCoord…
it=list.erase(it);
else
++it;
}
or, to make this look even more like C++, rewrite this as a single call to list.erase(remove_if(list.begin(), list.end(), functor), list.end());, where functor is a function or a function object that executes your checkHit.
Also, note that if you're erasing from the middle of a vector a lot, a different standard container might be more efficient, depending on what other operations you're doing to it: either std::list or one of the associative containers.