I am making a program for class that manages a Hotel. I am able to successfully check-in a customer into a room. But when I try to check-out a customer from a room, I get a run-time error: vector iterator incompatible. I ran the debugger, and says the problem is in the condition statement of my while loop, but I cant figure out what the problem is (I think I used the debugger correctly). I tried looking at other post with this similar error but I was not able to find a solution. Can anyone help?
void Customer::removeRoomID(int rID)
{
vector<int>::iterator iter;
iter = roomsCheckedInto.begin();
while(iter != roomsCheckedInto.end()) // <--DEBUGGER SAYS ERROR IN THIS LINE - ERROR: VECTOR ITERATOR INCOMPATIBLE
{
if(*iter==rID)
{
roomsCheckedInto.erase(iter);
}
}
}
std::vectoriterators are invalidated once aneraseoperation has been performed. (See reference here)Try changing your code to: