请问,其定义的格式如何?假如.h文件如下template <class type1, class type2> #ifdef DLL_FILE class _declspec(dllexport) A #else class _declspec(dllimport) A #endif { public: BOOL Create();
}; 对于Create成员函数的实现是否如下template <class ItemType, class EndBlockType> BOOL PbItemFile<ItemType, EndBlockType>::Create() { ... return TRUE; }在evc中编译可以通过,在workspace中双击该类的Create函数名,不能跳转到相对应cpp文件的该函数定义处, 显示出“Cannot find the definition(implementation) of this function”的提示是否可以说明,改函数的实现(如上代码格式)有问题
如上,在测试程序中定义一个该模板类的对象,不会出问题,一旦调用该模板类的函数,就出错 error LNK2019: unresolved external symbol "public: int __thiscall PbItemFile<char,char>::Create(void)" (?Create@?$PbItemFile@DD@@QAEHXZ) referenced in function ...
建议改成lib
#ifdef DLL_FILE
class _declspec(dllexport) A
#else
class _declspec(dllimport) A
#endif
{
public:
BOOL Create();
};
对于Create成员函数的实现是否如下template <class ItemType, class EndBlockType>
BOOL PbItemFile<ItemType, EndBlockType>::Create()
{
...
return TRUE;
}在evc中编译可以通过,在workspace中双击该类的Create函数名,不能跳转到相对应cpp文件的该函数定义处,
显示出“Cannot find the definition(implementation) of this function”的提示是否可以说明,改函数的实现(如上代码格式)有问题
error LNK2019: unresolved external symbol "public: int __thiscall PbItemFile<char,char>::Create(void)" (?Create@?$PbItemFile@DD@@QAEHXZ) referenced in function ...
找到出问题的原因
http://hi.baidu.com/woiwojia/blog/item/d5ef994ee2499aced0c86a24.html
内容如下今天写程序,将模板内的定义放在了.h文件中,而实现放在了.cpp中,编译通过了,可连接怎么也不能成功。。查了一些书才知道,模板类的定义和实现必须放在同一文件,《c++编程思想》中说:模板类定义很特殊,由template<...>定义的任何东西都意味着编译器在当时不为它分配内存空间,它一直处于等待状态,直到被一个模板实例告知,即模板参数是由编译器来替换的。
为了容易使用,几乎总是在头文件中放置全部的模板声明和定义。有时,也可能为了满足特殊需要而要在独立的cpp中放置模板的实现。但大部分现在的编译器还不支持模板类的定义和实现分开