I have a vector players which I would like to iterate over. The ‘player’ struct appears as follows:
struct player {
string name;
int rating;
};
I am using the iterator in a for loop to see if any of the ‘players’ have the name ‘playerName’ (a string).
for (vector<player>::iterator itr = players.begin(); itr != players.end(); ++itr) {
if (playerName.compare(*itr->name) == 0) return true;
}
return false;
Unfortunately, I keep running into an error: ‘error C2100: illegal indirection’ (visual C++ 2008). I believe that I’m dereferencing the iterator incorrectly; is there a better way to do so?
*itr->nameis equivalent to(*itr)->name.In your code, the type of
(*itr)isplayer, notplayer*, so the compiler is basically telling you that you’re trying to dereference something that isn’t a pointer.The correct way of doing that would be
(*itr).nameoritr->name, both meaning “dereference itr then access name”.