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.
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, eg
class IArpPacketBuilder
{
public:
IArpPacketBuilder(const DslPortId& aPortId) {}
virtual ~IArpPacketBuilder() = 0;
// Other abstract (pure virtual methods) here...
};
IArpPacketBuilder::~IArpPacketBuilder() {}
You need to provide definitions - ie code bodies for both the constructor and destructor for the abstract interface class - both functions will be used in your code, even though the class is abstract. An abstract class is not one which is never instantiated - it is one that is never directly instantiated by the user. It will however be instantiated by the compiler, which needs the constructor and destructor to be defined.
尝试内联它们-对我有用,尽管不知道这是否是一个好的解决方案
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.