解决方法:
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;
return 0;
}
3. 如下:
#include <iostream>
using namespace std;
//1.需要先声明类模板
template <class T>
class Compleax;
//2.再声明友元函数
template <class T>
ostream & operator<< (ostream &out, Compleax<T> &c);
template <class T>
class Compleax
{
//3.类中定义友元函数,注意需要加上<>
friend ostream & operator<< <T>(ostream &out, Compleax &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;
return 0;
}
这样即可解决错误提示为无法解析的外部符
之类的问题。下面来说明一下为什么会有这样的现象发生。
其实在模板机制的内部,编译器做的工作和没有模板机制时我们做的功能大同小异。在使用了函数模板的代码中。编译器其实进行了二次编译,第一次是在声明函数模板时,会检查你的函数模板的语法有没有错误,然后编译你的函数头,之后代码中使用模板时,就会把函数模板的代码根据变量类型来编译后并实例化,完成二次编译。
出现这种问题的原因就是,两次编译的函数头不一样,因为友元函数并不属于类的成员函数,所以需要单独声明此友元函数是函数模板,如果没有声明,但是后面在实现的时候又使用了template <class T>
,就会导致错误的发生。所以需要额外使用template <class S>
声明。
加上<T>
的目的是告诉编译器当前函数需要使用函数模板并且参数类型使用当前模板类的参数类型。