CAN'T FIGURE IT -- structs on a linked list

This is a discussion on CAN'T FIGURE IT -- structs on a linked list within the C++ Programming forums, part of the General Programming Boards category; hey all--
i'm new to c++ (as the difficulties below will undoubtedly prove) and would GREATLY appreciate any/all help i ...

(basically trying to take a Process struct off the newQ and place it on the readyQ)

does anyone have any idea what i'm doing wrong here? is the problem with the iterator? if so, what kind of pointer should i be using? i need to be able to iterate through the various lists, taking nodes off one list and placing them on another.

It's often tricky to iterate through a std::list using a for loop if you are removing elements. By erasing the node referred to by the iterator, you are invalidating the iterator. To use a popular analogy, you are cutting off the branch you're sitting on.

std::list::erase() returns the following iterator so that you can continue to iterate through the list. It's easier using a while loop -

ginoitalo's solution won't work for the same reason as yours. As soon as std::list::erase() is called the iterator is invalidated. His code only appears to work beccause his for loop never calls std::list::erase().

Also, you should always use the pre-increment operator with iterators rather than the post increment to prevent a temporary.