Search code examples
c++listiteratoroperator-precedence

Using Iterators on a list of pointers


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?


Solution

  • You have an issue with operator precedence, try adding parentheses

    (*it)->get_name()