I am trying to iterator over a list
of pointers:
int main () {
list<Game*> Games;
Games = build_list_from_file(); //Reading the games.info file
list<Game*>::iterator it = Games.begin();
it++;
cout << *it->get_name() << endl ;
// ...
}
When I compile it, I have this error:
error: request for member ‘get_name’ in ‘* it.std::_List_iterator<_Tp>::operator-><Game*>()’, which is of pointer type ‘Game*’ (maybe you meant to use ‘->’ ?)
cout << *it->get_name() << endl ;
^
Game
is a class that has the get_name
member function, which returns the name of the game. What should I do to make this compile?
You have an issue with operator precedence, try adding parentheses
(*it)->get_name()
You've run into an operator precedence issue. ->
has higher precedence than *
, so you're really doing:
*(it->get_name())
which doesn't compile since Game*
doesn't have any members, much less get_name
. You need to do the dereference first, which needs to be parenthesized:
(*it)->get_name()
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With