【问题标题】:Why is this a compiler error? (g++)为什么这是编译器错误? (g++)
【发布时间】:2011-04-27 23:40:47
【问题描述】:

所以我尝试在我的实用程序类中使用通用比较函子。

我尝试定义它并这样称呼它

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
}

预期会出现特定的编译器错误;在插入节点值之前

topValue 和 cmp_result 重复出现此错误;

我真的不明白为什么这是一个语法错误,我正在处理这个参考: http://www.cplusplus.com/reference/std/functional/binary_function/

【问题讨论】:

  • 为什么这一切:std::binary_function&lt;T,T,bool&gt;::first_argument_type?而不仅仅是T?

标签: c++ g++ compiler-errors


【解决方案1】:

它是一个从属名称,所以它需要typename 关键字:

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

对其他人也是如此。见the SO FAQ entry on dependent names

【讨论】:

    【解决方案2】:

    鉴于这些是依赖类型,您的第一步可能应该是添加typename

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

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2012-03-29
      • 2012-02-08
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2019-11-15
      • 1970-01-01
      相关资源
      最近更新 更多