0

I'm trying to print the names of some instances of Player, which is stored in a List<Player> players. What should I replace "Plop!" with to get this to work?

   list<Player>::iterator it;
   for(it=players.begin(); it != players.end(); ++it) cout << "Plop!" << " ";
   cout << endl;

I have tried

*it.getName();
*it->getName();

I have a feeling that the iterator should be handled differently than if it would be a normal pointer. Or perhaps the iterator *it does not contain the Player object at all?

1
  • As an alternative solution, consider implementing operator<< for Player class. Either as an overload or a friend function. Commented Jan 12, 2012 at 21:33

3 Answers 3

1

It should be;

(*it).getName()

the . operator binds harder than *, which makes the compilation fail otherwise.

Sign up to request clarification or add additional context in comments.

Comments

1
it->getName();

as long as Player has this method...

Comments

0

you need to dereference the iterator, as you're doing

*it

but the dereferencing drops to the left, if you know what i mean :) so you need to keep it in place with some parenthesis:

(*it).getName();

and don't forget the curly braces in the for loop!

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.