#include<iostream>
using std::cout;
using std::endl;
using std::ostream;
template<typename T>
class Complex;//进行类的前置声明
template<typename T>
ostream &operator<<(ostream &out,const Complex<T> &c3);
template<typename T>//友元函数的前置声明
class Complex
{
public:
Complex(T ,T );
void printCom();
Complex<T> operator+(Complex &c2);
friend ostream &operator<< <T>(ostream &out,const Complex<T> &c3);
private:
T a;
T b;
};
当没有进行前置声明时,会报该错误 error: declaration of ‘operator<<’ as non-function
深究其具体原理,是在编译生成汇编文件时出了问题
而类模板,是在编译时生成两次,第一次生成类头,第二次实例化。
而友元运算符重载,是全局函数,导致模板类无法找到对应的友元函数。
所以需要进行前置声明,进行唯一前置绑定操作。