简体   繁体   中英

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

Error   5   error LNK2001: unresolved external symbol "public: static void __cdecl AutomationDomainTypeBase::deleteType<class AutomationDomainTypeBase,1018>(unsigned int)"

AutomationDomainTypeBase

I'm guessing the Compiler cannot recognize that the Implemetation of deleteType is already in the Parent-Class. Because of having more then 400 child-Classes, i'm looking for a way of not implementing this function in all children.

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. 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.

Also, I think you can simply define OpcUa_NodeInstance::deleteType() as follows:

template <typename BaseType>
class OpcUa_NodeInstance
{
public:
    template <unsigned PrefixID>
    static void deleteType(unsigned int ObjID);
};

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM