Reputation: 691
I have been looking for the same problem I have with iterators; and I found a lot of topics with almost the same title, and similar problems, but mine is a little bit different.
I'm trying to get the *iterator at a specific position, but I get the error: "list iterator not deferencable" at run time, here is my code:
CompetidorFormula2000* retornarCompetidorF2000(int pos){
list<Competidor*>::iterator itr=miLista.begin();
CompetidorFormula2000* f1=new CompetidorFormula2000();
if(pos>0 && pos<=miLista.size()){
advance(itr,pos);
}
f1=(CompetidorFormula2000*) (*itr);
return f1;
delete f1;
}
Upvotes: 0
Views: 6047
Reputation: 81349
If pos
equals miLista.size()
then you will get an iterator to the end of the list, the same iterator you get when you call miLista.end()
. An iterator to the end of the list is not dereferenceable.
Upvotes: 3