Problem with std::list
I am trying to move some objects around in a std::list while looping through it. It works, but if isActive() returns false, the program crashes.
I am sure it has something to do with how I move the data around in the std::list, but I am not sure how to fix it. Could someone please give me a hand?
// Some code to add things to the list...
std::list<Object*>::iterator it = myList.begin();
// Update all the active objects in the list
while (it != myList.end())
// Check if the object is active
// Update the object, if update failes, move it to the front
// of the list.
if( !( (*it)->update() ) )
// Assing the current object to a temp itterator
tempIt = it;
// Move the main itterator to the next object
// Insert the object that failed the update to the start of the list
Should your else it++ go with the big if (isActive) instead of the little if (update)? Otherwise you just spin until isActive magically becomes true.
Thanks makes sense.
I changed my code, but now it stops at once.
What do you mean by "stops"?
Anyway, I think you have splice backwards, if I'm reading the manual correctly. You're moving the head of the list into the spot marked by tempIt, not the other way around. Maybe you want
myList.splice(myList.begin(), myList, tempIt);
Again, this was a case of me being stupid.
if isAcitve is true, it++ will never happen. Also, thanks for correcting my splice function