PDA

View Full Version : can't fetch vector -> pointer -> object -> method using *iterator->method(); (C++)


shlagish
11-03-2009, 12:21 AM
This is part of a code I'm writing as a school assignment. The problem has nothing to do with the homework topic (semaphores).

Here is what I have:

void Jeu::lancer()
{
vector<Joueur*>::iterator joueur = joueurs_.begin();
while(joueur != joueurs_.end())
{
*joueur->jouer(); // This is the tricky bit
++joueur;
}

}

Jeu is a class which has a private member joueurs_ which is a vector of pointers to objects of class Joueur. It is declared like this:
vector<Joueur*> joueurs_;
The public method lancer() is supposed to call, for every Joueur in vector joueurs_, it's own public method jouer().
This is the compilation error I get, which I don't quite understand..

jeu.cpp:20: error: request for member ‘jouer’ in ‘* joueur.__gnu_cxx::__normal_iterator<_Iterator, _Container>::operator-> [with _Iterator = Joueur**, _Container = std::vector<Joueur*, std::allocator<Joueur*> >]()’, which is of type ‘Joueur*’ which is not a class
This is translated from spanish so the words might not be exact.,,

I think I might not be fetching the object correctly, but I tried many different ways, and none of the following work:

*joueur->jouer();
**joueur->jouer();
***joueur.jouer();


Does anybody understand what I'm doing wrong here?

Thanks a bunch in advance

oracleguy
11-03-2009, 12:33 AM
You are missing parenthesis.

(*joueur)->jouer();

shlagish
11-03-2009, 08:41 PM
Well thank you very much, it does work.
I guess the problem was it was trying to do the -> before the *...
Thus trying to access the method of that which is pointed to by the iterator, and seeing what the return value points to.

Are expression always evaluated from right to left like this? (apart from arithmetic expressions of course)

oracleguy
11-04-2009, 12:04 AM
Are expression always evaluated from right to left like this? (apart from arithmetic expressions of course)

No but there is operator order, see: http://www.cppreference.com/wiki/operator_precedence