模板类的小问题(链接失败)
今天上午做模板类。结果还发现了大大小小的问题。
希望自己的模板变的越来越规范,所以强迫自己把模板类中的函数的定义和声明分开。 而且放在了不同的文件里。
下面是大概的写法。
template < class type >
class A
  … {
public :
friend ostream & operator << (ostream & , A < type >& );
A();
…
 } ;
然后在另一个.cpp中这样定义
template < class type >
ostream & operator << (ostream & os,A < type >& a)
  … {
os << a… << endl;
return os;
 }
template < class type >
A
< type > ::A()
  … {
…
 }
template < class type >
void A < type > ::test()
  … {
…
 }
在main函数中这样调用
A
a.test();
cout << a;
结果链接失败…报的错误好像是link2019.
不清楚为啥,后来同时在main的文件中include A.h 和A.cpp可以解决构造函数和Test()函数reference的问题。
然后又将重载<<的函数放在类的定义中才解决了link2019链接错误.
但是我还是想把这个重载的函数声明和定义分开写,不知道哪位高手可以指点一下。