1

given that class:

#include <iostream>
using std::ostream;

template<class T>
class MyClass {
    T x;
public:
    MyClass(T x);
    friend ostream& operator<<(ostream& os, const MyClass<T>& m);
};

template<class T>
MyClass<T>::MyClass(T x) :
        x(x) {
}

template<class T>
ostream& operator<<(ostream& os, const MyClass<T>& m) {
    return os < m.x;
}   

I get the following error:

friend declaration 'std::ostream& operator<<(std::ostream&, const MyClass&)' declares a non-template function

Can someone explain the meaning of that error and how can I fix it (but I want to do that inside the class)?

Mohammad Kanan
  • 4,452
  • 10
  • 23
  • 47
AskMath
  • 415
  • 1
  • 4
  • 11

0 Answers0