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!

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值