解决方法:
1. 直接把定义和实现都写在类中
2. 如下:
#include <iostream>
using namespace std;
template <class T>
class Compleax
{
template <class S> //注意这里是S,和上面的T的名字不一样
friend ostream & operator<< (ostream &out, Compleax <S>&c);
public:
Compleax(T a, T b);
void PrintCom()
{
cout<<"a:"<<a<<" b:"<<b<<endl;
}
private:
T a;
T b;
};
template <class T>
Compleax<T> :: Compleax(T a, T b)
{
this->a = a;
this->b = b;
}
template <class T>
ostream &operator << (ostream &out, Compleax<T> &c)
{
out<<c.a<<" "<<c.b<<endl;
return out;
}
int main()
{
Compleax<int> c(2, 3);
c.PrintCom();
cout<<c<<endl;

本文介绍了解决模板类友元函数无法解析外部符号错误的方法,包括直接在类内定义和实现,以及如何正确声明和定义模板友元函数。
最低0.47元/天 解锁文章
5538





