嵌套节点类运算符重载< C ++

时间:2014-08-18 15:24:12

标签: c++ linked-list operator-overloading nodes

我试图超载<我的LinkedList类中的嵌套Node类的运算符。 我有这样的设置:

LinkedList<T>::Node& LinkedList<T>::Node::operator<(const LinkedList<T>::Node& rhs){
    return rhs; 
}

但我得到错误 1>c:\users\kevin\workspace\linkedlist\linkedlist.h(185): warning C4183: '<': missing return type; assumed to be a member function returning 'int'

我尝试返回1,但这也不起作用。

1 个答案:

答案 0 :(得分:4)

Node是一个从属名称,因此您需要使用typename告诉编译器您指的是类型。

template <typename T>
const typename LinkedList<T>::Node& 
LinkedList<T>::Node::operator<(const typename LinkedList<T>::Node& rhs)

另外,请注意您有一个const引用,但是您返回的是非const引用。您应该返回const引用,但在实际代码中,operator<不返回bool会非常困惑。这会更有意义:

template <typename T>
bool LinkedList<T>::Node::operator<(const typename LinkedList<T>::Node& rhs) const
相关问题