在使用模板时遇到的error LNK2001: unresolved external symbol错误

 
Since you aren't using the export feature in your .cpp file, and I don't
think your compiler would support it, you have to including the member
definitions in the same header file as your class template definition.
What that means is that you have to copy all the member definitions from
the .cpp file into the .h file between your include guards and after the
class template definition. If you know about inline functions, its
mostly the same the idea.

    使用类模板的时候类的定义和类的实现不能分离,需要把类的实现代码也放到头文件中。

    究其原因,是因为类模板和函数模板都不是真正的定义,真正的定义是在模板实体化的时候由编译器完成的。如果将模板的定义部分和实现部分分离开来,编译器真正要去完成模板实体化的时候就会因为找不到相应的代码而发生链接错误。

    另外,据说C++标准是要求能实现分离编译的。只不过实现起来难度太高,所以大部分主流编译器(当然也包括VC在内)都不支持这个功能。

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值