我们是两个学生,我们现在有一个我们无法解决的史诗般的大问题。我们向老师求助,但他帮不了我们,所以我们最后的机会就是这个论坛!
我们正在做一个项目:NPI 文件的命令解释器。
map<string,void(Interpreteur::*)()>::iterator trouve = interpreteur.myMap.find(saisie);
if(trouve == interpreteur.myMap.end())
cerr<<"command not found"<<endl;
else
(trouve->*second)();
我们必须使用名为“map”的对象,但我们无法获取第二个参数,名为..“Second”。为什么?代码块告诉我们错误在“else”中,这是错误:
'second' was not declared in this scope.
我们也试过:
map<string,void(Interpreteur::*)()>::iterator trouve = interpreteur.myMap.find(saisie);
if(trouve == interpreteur.myMap.end())
cerr<<"command not found"<<endl;
else
(trouve.second)();
代码块回答:
error: 'std::map, void (Interpreteur::*)()>::iterator' has no member named 'second'
如果有人能帮助我们,这将挽救我们的项目,我们必须在明天结束它。我们将不胜感激。
非常感谢您的帮助,如果有问题我们可以回答:)
最佳答案
std::map
迭代器指向一对。因此,要访问该对的第二个元素,请执行以下操作:
trouve->second
请注意,在您的情况下,第二个元素的类型是“指向 Interpreteur
成员函数的指针”,因此要调用它,您需要提供一个 Interpreteur
目的。像这样:
(interpreteur.*(trouve->second))()
关于c++ - 如何访问第二个 map 迭代器?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/24057034/