2011-04-27 218 views
0

所以我試圖在我的工具類中使用泛型比較函子。這是一個編譯器錯誤? (g ++)

我試圖去定義它,並調用它像這樣

template <class T> 
bool AVL_tree<T>::avl_insert(AVL_Node<T> *& top, const AVL_Node<T> * insertNode, bool & taller) { 
    std::binary_function<T,T,bool>::first_argument_type insertNodeValue; 
    insertNodeValue = insertNode->data; 
    std::binary_function<T,T,bool>::second_argument_type topValue; 
    topValue = insertNode->data; 
    std::binary_function<T,T,bool>::result_type cmp_result; 
    cmp_result = comparer(insertNodeValue,topValue); 
    std::binary_function<T,T,bool>::result_type cmp_result2; 
    cmp_result2 = comparer(topValue,insertNodeValue); 
    //Function continues from here 
} 

具體的編譯器錯誤預期; insertNodeValue之前

對於topValue和cmp_result重複該錯誤;

我真的不明白爲什麼這是一個語法錯誤,我正在關閉此引用: http://www.cplusplus.com/reference/std/functional/binary_function/

+3

爲什麼這一切:'的std :: binary_function :: first_argument_type'?而不是隻是'T'? – 2011-04-27 23:46:19

回答

2

這是一個從屬名稱,所以它需要typename關鍵字:

typename std::binary_function<T,T,bool>::first_argument_type insertNodeValue; 

對其他人也一樣。見the SO FAQ entry on dependent names

0

鑑於這些依賴類型,第一步可能應該添加typename

typename std::binary_function<T,T,bool>::first_argument_type insertNodeValue;