template模板类必须将申明与实现写在一个文件中的原因

参考文献:http://www.cplusplus.com/doc/oldtutorial/templates/

其中有这么一段话:

Because templates are compiled when required, this forces a restriction for multi-file projects: the implementation (definition) of a template class or function must be in the same file as its declaration. That means that we cannot separate the interface in a separate header file, and that we must include both interface and implementation in any file that uses the templates.

我思考了一些,也做了一个实验,模板类在编译的时候并没有编译,因为没有指定其类型。所以如果分开声明与实现的话,在调用代码里面包含头文件,然后头文件回去编译过的库中找实现,因为没有编译这些模板类的成员,所以找不到,就会出现链接错误。

Done!

你可能感兴趣的:(C++学习)