class Complex
{
public:
friend ostream& operator<<(ostream &out, Complex &c3);
Complex(T a = 0, T b = 0);
void PrintfComplex();
Complex operator+(Complex &c);
Complex operator-(Complex &c);
private:
T a;
T b;
};
错误 1 error LNK2019: 无法解析的外部符号 "class std::basic_ostream<char,struct std::char_traits<char> > & __cdecl operator<<(class std::basic_ostream<char,struct std::char_traits<char> > &,class Complex<int> &)" (??6@YAAAV?$basic_ostream@DU?$char_traits@D@std@@@std@@AAV01@AAV?$Complex@H@@@Z),该符号在函数 _main 中被引用 D:\HQ\templatecomplex\templatecomplex\classtemplate.obj templatecomplex
问题本质:
模板是两次编译的,第一次生成的函数头和第二次生成的函数头不一样
错误处理:
class Complex
{
public:
friend ostream& operator<<<T>(ostream &out, Complex &c3);
Complex(T a = 0, T b = 0);
void PrintfComplex();
Complex operator+(Complex &c);
Complex operator-(Complex &c);
private:
T a;
T b;
};
添加红色位置内容