您的位置:首页 > 其它

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

2011-05-05 11:28 323 查看
将模板类的声明放在.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
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签:  types class 编译器 file
相关文章推荐