Zax Ftw
Zax Ftw

Reputation: 103

Inherit from non-specialized template (shared library)

I'm getting "Unresolved External Symbols" when I'm inheriting directly from shared library class template, but if I specialize library template in my code first, it works fine.

Template class in shared library:

template <typename T>
class EventHandler
{
public:
    virtual ~EventHandler();
    virtual EventResult ReceiveEvent(T * evn, EventDispatcher<T> * dispatcher) = 0; 
};

Derived class in my code (doesn't work without specialization):

class MyEventHandler : public EventHandler<SomeEventType>
{
public:
    virtual     EventResult    ReceiveEvent(SomeEventType * evn, EventDispatcher<SomeEventType> * dispatcher);
};

Specialized library template class in my code:

template <>
class EventHandler<SomeEventType>
{
public:
    virtual ~EventHandler() {}
    virtual EventResult ReceiveEvent(SomeEventType * evn, EventDispatcher<SomeEventType> * dispatcher) = 0;
};

I tried reproducing it directly in shared lib's code (I have the source) and it worked fine without template specialization. Does it have anything to do with lib not giving access to full class implementation? In this case EventHandler template doesn't have any additional definitions in .cpp because it's just one pure virtual method.

Also my derived classes are inside the namespace if it matters.

[EDIT] class template of EventHandler doesn't have any additional implementation in .cpp file, this definition posted above from .h file is everything it does (one pure virtual function). It has nothing to do with thread suggested as possible duplicate.

Upvotes: 2

Views: 117

Answers (1)

Michał Łoś
Michał Łoś

Reputation: 643

The reason is that you don't have destructor definition in your EventHandler class. Your specialization does overload it, so compiler is not left without definition. Note, that this is the code that have to be defined in header file, not in cpp file (thus, being a part of library binary), because compiler has to instantiate new definition for every type with which EventHandler template type is specialized/instantiated.

Upvotes: 5

Related Questions