1

new to operator overloading and I'm getting an unresolved exteneral error message when trying to operator overload with <<. Error LNK1120 and 2019. Anyone know why? Thank you!

template <class Type>
class ListType {
 public:
 .. 
 friend std::ostream& operator<< (std::ostream&, const ListType &);    
 };

 template <class Type>
 std::ostream& operator<< (std::ostream& out, const ListType<Type>& list) {
  if (list.head) {
    NodeType<Type> *temp = list.head;
    out << list.head->item;
    temp = temp->next;
    while (temp != 0) {
        out << "," << temp->item;
        temp = temp->next;
    }
  }
return out;
}
Zephers
  • 19
  • 4
  • GCC 6.3.0: warning: friend declaration `'std::ostream& operator<<(std::ostream&, const ListType&)'` declares a non-template function `[-Wnon-template-friend]` (if this is not what you intended, make sure the function template has already been declared and add `<>` after the function name here) – xinaiz Feb 19 '17 at 20:52
  • Posted example doesn't provoke the linker error itself. Is this code placed in a header or source file? – xinaiz Feb 19 '17 at 20:58
  • [second answer of this question](http://stackoverflow.com/questions/4014294/operator-overloading-on-class-templates) should explain how to do it properly – Fureeish Feb 19 '17 at 20:59
  • Ah sorry, this code is in the header file. When i go to main I have: #include #include "NodeType.h" #include "ListType.h" #include "OListType.h" #include "UListType.h" using namespace std; int main() { OListType list1; cout << list1; – Zephers Feb 19 '17 at 20:59
  • Just saw the link, looking now. Thank you – Zephers Feb 19 '17 at 21:02
  • [My answer to another question about overloading `operator+`](http://stackoverflow.com/a/35854179/434551) might be helpful. – R Sahu Feb 19 '17 at 21:05
  • Should probably be `template friend std::ostream& operator<< (std::ostream&, const ListType &);`, but post an MCVE, please. – Christian Hackl Feb 19 '17 at 21:12

0 Answers0