Search code examples
c++inheritanceinterfacelinkerunresolved-external

Unresolved symbol when inheriting interface


It's late at night here and I'm going crazy trying to solve a linker error.

If I have the following abstract interface:

class IArpPacketBuilder
{
public:

    IArpPacketBuilder(const DslPortId& aPortId);
    virtual ~IArpPacketBuilder();

    // Other abstract (pure virtual methods) here...
};

and I instantiate it like this:

class DummyArpPacketBuilder
    : public IArpPacketBuilder
{

public:

    DummyArpPacketBuilder(const DslPortId& aPortId)
        : IArpPacketBuilder(aPortId) {}
    ~DummyArpPacketBuilder() {}
};

why am I getting the following error when linking?

Unresolved symbol references:

IArpPacketBuilder::IArpPacketBuilder(DslPortId const&):
    ppc603_vxworks/_arpPacketQueue.o
IArpPacketBuilder::~IArpPacketBuilder():
    ppc603_vxworks/_arpPacketQueue.o
typeinfo for IArpPacketBuilder:
    ppc603_vxworks/_arpPacketQueue.o
*** Error code 1

IArpPacketBuilder is an abstract interface, so as long as I define the constructors and destructions in the concrete (derived) interface, I should be fine, no? Well it appears not.


Solution

  • You have only declared the constructor and destructor of IArpPacketBuilder, not defined them. The linker needs the definitions too. Note that C++ has no concept of abstract interface - IArpPacketBuilder is a plain old class which happens to contain some pure virtual methods, thus making its direct instantiation impossible.

    So the simplest solution is to provide inline implementations:

    class IArpPacketBuilder
    {
    public:
    
        IArpPacketBuilder(const DslPortId& aPortId) {}
        virtual ~IArpPacketBuilder() {}
    
        // Other abstract (pure virtual methods) here...
    };
    

    You can also make the destructor pure virtual, but even so, you still need to provide a definition for it, e.g.

    class IArpPacketBuilder
    {
    public:
    
        IArpPacketBuilder(const DslPortId& aPortId) {}
        virtual ~IArpPacketBuilder() = 0;
    
        // Other abstract (pure virtual methods) here...
    };
    
    IArpPacketBuilder::~IArpPacketBuilder() {}