这个错误`friend declaration'是什么意思std :: ostream& operator<<(...)'声明一个非模板函数`?

时间:2018-06-05 22:30:41

标签: c++ c++11 templates friend ostream

鉴于该课程:

#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;
}   

我收到以下错误:

  

朋友声明'std :: ostream&amp; operator&lt;&lt;(std :: ostream&amp;,const MyClass&amp;)'声明一个非模板函数

有人可以解释该错误的含义以及如何解决它(但我想在课堂上这样做)?

0 个答案:

没有答案
相关问题