c++的类模板

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);
}

而+-*/等操作符的友元重载则需要定义在类声明内部,否则连接会出错。

This entry was posted in Program and tagged , , , . Bookmark the permalink.

发表评论

电子邮件地址不会被公开。 必填项已用 * 标注

*

您可以使用这些 HTML 标签和属性: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>

Please copy the string Xu92nl to the field below:

以新浪微博帐号登录