【发布时间】:2016-05-04 11:44:29
【问题描述】:
我有一个非常具体的问题(不要介意问我为什么要这个,解释起来会很复杂)
我想从一个父类调用一个模板函数,它间接调用了子类的析构函数
我尝试实现这段代码:
父类:
template <typename BaseType> //OpcUa_NodeInstance.h
class OpcUa_NodeInstance
{
public:
template <typename BaseTypee, unsigned PrefixID>
static void deleteType(unsigned int ObjID);
};
template <typename BaseType> // OpcUa_NodeInstance.cpp
template <typename BaseTypee, unsigned PrefixID>
void OpcUa_NodeInstance<BaseType>::deleteType(unsigned ObjID)
{
if (ObjID == PrefixID)
{
NodeManagerRoot* pNodeManagerRoot = NodeManagerRoot::CreateRootNodeManager();
auto dummyTypeInstance = new BaseTypee(UaNodeId(PrefixID, 2),
UaString("Dummy_AutomationDomainType"), 2, pNodeManagerRoot);
delete dummyTypeInstance;
}
}
子类:
class AutomationDomainTypeBase: // AutomationDomainTypeBase.h
public OpcUa_NodeInstance<AutomationDomainTypeBase>
{
public:
template <typename BaseType, unsigned int PrefixID>
static void deleteType(unsigned int ObjID);
}
问题是 Visual Studio 显示链接器错误
Error 5 error LNK2001: unresolved external symbol "public: static void __cdecl AutomationDomainTypeBase::deleteType<class AutomationDomainTypeBase,1018>(unsigned int)"
AutomationDomainTypeBase
我猜编译器无法识别 deleteType 的实现已经在父类中。由于有超过 400 个子类,我正在寻找一种不在所有子类中实现此功能的方法。
【问题讨论】:
-
您提供的代码在我看来确实编译得很好,使用 ms vc14。
-
@shrike 谢谢,我忘了包含 Parent.h,我编辑了我的主题。你能帮我解决链接器错误吗?
-
template <typename BaseType> template <typename BaseTypee是这里的输入错误还是您的代码输入错误?首先是 Type,然后是 Typee。 -
@bipll 如果我将类模板和函数模板命名为相同,我会收到此错误:c3860
-
哦,当然,这两个是不同的类型变量。
标签: c++ templates inheritance crtp