Jérémy
Jérémy

Reputation: 1890

How to access to the second map iterator?

We are two students and we now have an epic big problem that we can't resolve. We asked our teacher for some help but he can't help us, so our last chance is this forum!

We're doing a project: a command interpreter of NPI files.

map<string,void(Interpreteur::*)()>::iterator trouve = interpreteur.myMap.find(saisie);
if(trouve == interpreteur.myMap.end()) 
    cerr<<"command not found"<<endl; 
else 
    (trouve->*second)();

We must use the object named "map" but we can't get the second parameter, named.. "Second". Why? Code Blocks told us the error is in the "else", here is the error:

'second' was not declared in this scope.

We have tried too:

map<string,void(Interpreteur::*)()>::iterator trouve = interpreteur.myMap.find(saisie);
if(trouve == interpreteur.myMap.end()) 
    cerr<<"command not found"<<endl; 
else 
    (trouve.second)();

And code blocks answered:

error: 'std::map, void (Interpreteur::*)()>::iterator' has no member named 'second'

If someone can help us, it will save our project, we must end it for tomorrow.. We will be very grateful.

Thank you very much for help, we can answer questions, if there are any :)

Upvotes: 8

Views: 3331

Answers (1)

A std::map iterator points to a pair. So to access the pair's second element, do this:

trouve->second

Note that in your case, the type of that second element is "pointer to member function of Interpreteur," so to call it, you need to provide an Interpreteur object. Something like this:

(interpreteur.*(trouve->second))()

Upvotes: 5

Related Questions