2

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.

Zax Ftw
  • 81
  • 4
  • 10

1 Answers1

5

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.

Michał Łoś
  • 633
  • 4
  • 15
  • 1
    But why it didn't require specialization if I implemented `MyEventHandler` directly in shared lib's code? Destructor of `EventHandler` still wouldn't have definition, but it compiled without problems. – Zax Ftw Jan 22 '18 at 14:47
  • @ZaxFtw: With the code you've presented here, I don't think it's possible - I'm guessing that, for example, your shared library have it's definition hidden in some internal header file accidentally, where it was visible to the MyEventHandler implementation, but not to the code outside of your library. Here is the code on coliru: http://coliru.stacked-crooked.com/a/a0929e3c490d918c – Michał Łoś Jan 22 '18 at 19:24
  • Added empty destructor definition to template code `~EventHandler() {}` but accidentally removed `= 0;` what made the function not pure and the class not abstract anymore, and for some unknown reason it didn't work. Now when both are back (zero and destructor) it's finally compiling. – Zax Ftw Jan 22 '18 at 22:50