[英]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? IArpPacketBuilder
是一个抽象接口,所以只要我在具体的(派生的)接口中定义构造函数和销毁方法,我就可以了,不是吗? Well it appears not. 好吧,看来不是。
You have only declared the constructor and destructor of IArpPacketBuilder
, not defined them. 您仅声明了IArpPacketBuilder
的构造函数和析构IArpPacketBuilder
,而未定义它们。 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. 请注意,C ++没有抽象接口的概念IArpPacketBuilder
是一个普通的旧类,它碰巧包含一些纯虚方法,因此无法直接实例化。
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. 但是它将由编译器实例化,需要定义构造函数和析构函数。
尝试内联它们-对我有用,尽管不知道这是否是一个好的解决方案
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.