c++的类模板
Posted on 十二月 16th, 2008 in Program | No Comments »
c++的类模板中有一些要注意的地方:
1、除非编译器实现了export关键字,否则将模板成员函数放置在一个独立的实现文件中将无法运行。(在GCC中,就是无法连接,编译可以通过)
因为模板不是函数,它们不能单独编译。模板必须与特定的模板实例化请求一起使用。
2、在类模板的操作符重载的友元函数中,要先声明,如<<操作符,要在函数后添加一对<>符合:
template <class T>
class Matrix;
template <class T>
ostream& operator <<(ostream& os,const Matrix<T>& matrix);template <class T>
class Matrix
{
public:
friend ostream& operator << <>(ostream& os,const Matrix<T>& matrix);
}
而+-*/等操作符的友元重载则需要定义在类声明内部,否则连接会出错。