I'm trying to remove an element from a vector while iterating over it. Initially I was using a simple for i = 0, i < vector.size() but it wouldn't let me call remove(i) as i was an int, not an iterator.
So I've changed it to for(vector<Switch>::iterator i = switchVector.begin(); i != switchVector.end(); i++){ however when the condition to remove an element is met and switchVector.erase(i) is called I get an error: "Expression: vector iterator not dereferencable".
I've searched but can't seem to find a solution to this problem.
Thanks.
That is because you are trying to dereference switchVector.end(). That's an iterator beyond the last element of the vector, and therefore not dereferencable.
The reason why you reach .end() is that you also increment i if you erase something. That's wrong, because when you erase, all elements are shifted one place to the left, so i already points to the next element.
If to speak shortly your code is so awful that the best advice is to throw out such a code. It does not deserve that somebody spend his time reviewing it.