[英]Calling Template-Function of Parent-Class from Derived Class
I have a very specific question (dont mind asking why i want this, it would be very complicated to explain it) 我有一个非常具体的问题(不要介意问我为什么要这样做,解释起来会很复杂)
I want to call a Template-Function from a parent-Class, which indirectly calls the Destructor of the Child-Class 我想从父类调用一个模板函数,它间接调用子类的析构函数
I tried to implement this code: 我试图实现以下代码:
The Parent Class: 家长班:
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;
}
}
The Child Class: 儿童班:
class AutomationDomainTypeBase: // AutomationDomainTypeBase.h
public OpcUa_NodeInstance<AutomationDomainTypeBase>
{
public:
template <typename BaseType, unsigned int PrefixID>
static void deleteType(unsigned int ObjID);
}
the problem is that visual studio shows a linker error 问题是Visual Studio显示链接器错误
Error 5 error LNK2001: unresolved external symbol "public: static void __cdecl AutomationDomainTypeBase::deleteType<class AutomationDomainTypeBase,1018>(unsigned int)"
AutomationDomainTypeBase AutomationDomainTypeBase
I'm guessing the Compiler cannot recognize that the Implemetation of deleteType is already in the Parent-Class. 我猜编译器无法识别出DeleteType的实现已经在父类中。 Because of having more then 400 child-Classes, i'm looking for a way of not implementing this function in all children.
因为有超过400个子类,所以我正在寻找一种不在所有子类中实现此功能的方法。
You are trying to define the child class without including first definition of the parent class. 您试图定义子类而不包含父类的第一个定义。
If I compile your code with both definitions, I got no compilation error. 如果我用这两个定义编译您的代码,则没有编译错误。
But if I compile only the child class definition, without including parent class definition first, I got: 但是,如果我仅编译子类定义,而没有先包含父类定义,则得到:
1>Time\Time_Test.cpp(625): error C2504: 'OpcUa_NodeInstance' : classe de base non définie
1>Time\Time_Test.cpp(625): error C2143: erreur de syntaxe : absence de ',' avant '<'
just like you. 就像你一样。
Note : make sure OpcUa_NodeInstance<BaseType>::deleteType()
template member function is properly defined (and not declared only) in the header file where you define OpcUa_NodeInstance
template class. 注意 :请确保在定义
OpcUa_NodeInstance
模板类的头文件中正确定义了OpcUa_NodeInstance<BaseType>::deleteType()
模板成员函数(并且不仅声明了)。 Otherwise, you will get an undefined symbol at linking. 否则,您将在链接时获得未定义的符号。
New edit: 新编辑:
OK, I think I got what you need: just do not declare/define AutomationDomainTypeBase::deleteType()
if you want only OpcUa_NodeInstance::deleteType()
to be used for any child class. 好的,我想我满足了您的需求:如果您只希望将
OpcUa_NodeInstance::deleteType()
用于任何子类,则不要声明/定义AutomationDomainTypeBase::deleteType()
。
Also, I think you can simply define OpcUa_NodeInstance::deleteType()
as follows: 另外,我认为您可以按以下方式简单地定义
OpcUa_NodeInstance::deleteType()
:
template <typename BaseType>
class OpcUa_NodeInstance
{
public:
template <unsigned PrefixID>
static void deleteType(unsigned int ObjID);
};
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.