0

I'm trying to overload the < operator for my nested Node class inside my LinkedList class. I have it setup like this:

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

But I just get the error 1>c:\users\kevin\workspace\linkedlist\linkedlist.h(185): warning C4183: '<': missing return type; assumed to be a member function returning 'int'

I try to return 1 but that doesn't work either.

kevorski
  • 721
  • 1
  • 7
  • 26

1 Answers1

4

Node is a dependent name, so you need to use typename to tell the compiler you are referring to a type.

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

Also, note that you have a const reference, but you were returning a non-const one. You should return a const reference, but in real code, it would be very confusing for operator< not to return a bool. This would make more sense:

template <typename T>
bool LinkedList<T>::Node::operator<(const typename LinkedList<T>::Node& rhs) const
juanchopanza
  • 210,243
  • 27
  • 363
  • 452
  • should this be going inside the node class or inside the linkedlist class? – kevorski Aug 18 '14 at 15:32
  • 1
    @kevorski This is a definition, it goes outside of both classes. The declaration goes inside if the `Node` class definition. You can also put the definition inside there, but you'd have to ge tthe syntax right. – juanchopanza Aug 18 '14 at 15:41