I am trying to delete a object from a vector at a specific index. The vector iterator keeps track of the index throughout the program. In the below code, the first IF statement works perfectly. But, if the iterator is pointing to anywhere OTHER than the last element, I erase the element from the vector and then increment the iterator. The program crashes and says "iterator not incrementable".
I ran the debugger several times and everything looks correct, so I cannot see what I am missing?
vector<Card> myVector; //container to hold collection of cards. vector<Card>::iterator myVectorIterator; //points to each "card" in the collection. Card Collection::remove() { if (myVectorIterator== myVector.end()-1) { //at the last card //erase the "current" card myVector.erase(myVectorIterator); //update to the first card. myVectorIterator= myVector.begin(); } else { myVector.erase(myVectorIterator); //crashes here! myVectorIterator++; } return *myVectorIterator; }