参考文献: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++中模板的编译特性,特别是对于多文件项目的影响。指出模板的实现必须与声明在同一文件中,无法将其接口分离到单独的头文件,这导致在使用模板时必须在所有相关文件中同时包含接口和实现。
336

被折叠的 条评论
为什么被折叠?



