2014-06-05 72 views
6

我們是兩個學生,現在我們有一個史詩般的大問題,我們無法解決。我們問老師一些幫助,但他不能幫助我們,所以我們最後的機會是這個論壇!如何訪問第二個映射迭代器?

我們正在做一個項目:一個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」的第二個參數。爲什麼?代碼塊告訴我們的錯誤是在 「其他」,這裏是錯誤:

'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'

如果有人可以幫助我們,它會拯救我們的項目,我們必須明天結束它..我們將非常感激。

非常感謝你們的幫助,我們可以回答的問題,如果有任何:)

回答

5

一個std::map迭代器指向一對。所以訪問該對的第二個元素,這樣做:

trouve->second 

注意,在你的情況下,第二個元素的類型是「指向的Interpreteur成員函數,」這麼稱呼它,你需要提供一個Interpreteur對象。類似這樣的:

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