模板类中方法的声明与定义 unresolved external symbol

将模板类的声明放在.h中, 实现放在.cpp中结果出现unresolved external symbol;

解决办法:

  • 包含编译模式:

1.将实现一起放在.h中

  • 分离编译模式

2.在.h中使用export(编译器要支持, 很多貌似不支持)

// ----- Queue.h -----
// 声明 Queue 是一个可导出的 (exported) 类模板
export template <class Type>
class Queue {
 // ...
public:
 Type& remove();
 void add( const Type & );
 // ....
};

 

3.在.cpp中加入你想实例化的类型的显式声明,你必须提前知道那种类型的实例化是你想要的(Another option is to put the code in the cpp file and in the same cpp file add explicit instantiations of the template with the types you expect to be using. This is useful if you know you're only going to be using it for a couple of types you know in advan//ce.)

// 显式实例声明
template class Queue<int>;

 

4.在.h中包含.cpp:在.h后面#include“Queue.cpp”(但是我试的时候, 有错误提示重复定义,不知道怎么弄)maybe add inline

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值