2013-11-26 46 views
-1

我有许多对象,它们都具有不带参数的构造函数。我定义了基类的简单构造函数,而我没有定义其他的。编译时,我收到每个类的“Undeclared reference to constructor”错误,但如果我尝试定义派生对象的构造函数,则会出现更令人困惑的错误。 以下是“怪异”错误:对派生类构造函数的未声明引用

CMakeFiles/project4.dir/src/Strategy.cpp.o: In function `ForwardStrategy': 
/home/ics45c/projects/p4/src/Strategy.cpp:42: undefined reference to `vtable for 
ForwardStrategy' 

我的基类的构造函数如下:

ForwardStrategy::ForwardStrategy() 
{ 
} 

和所有其他的构造看起来像这样(每个不同的号码):

ForwardStrategy1::ForwardStrategy1() 
{ 
} 

任何帮助非常感谢!

编辑:这里是类声明:

class ForwardStrategy 
{ 
public: 
    ForwardStrategy(); 
    virtual ~ForwardStrategy() = default; 
    virtual bool isWorthForwarding(Message::Message* m) = 0; 
    virtual void setType(unsigned int type); 
    virtual void setQuality(unsigned int q); 
private: 
    unsigned int type; 
    unsigned int quality; 
}; 

回答

0

确保您的非纯虚方法某处实施,目前正在联系。请参阅GCC FAQ

Therefore, if you fail to define this particular method, the linker may complain about the lack of definitions for apparently unrelated symbols...

The solution is to ensure that all virtual methods that are not pure are defined. Note that a destructor must be defined even if it is declared pure-virtual.

+0

感谢您的回复。我仍然困惑于_which_方法来定义。我的基类声明了5个方法:一个构造函数,一个析构函数,两个void和一个bool。构造函数声明如下: ForwardStrategy() 其他方法: virtual〜ForwardStrategy()= default; virtual bool isWorthForwarding(Message :: Message * m)= 0; virtual void setType(unsigned int type); virtual void setQuality(unsigned int q); 我会在上面发表它们,以使它们更具可读性。 – MattDella

+0

请将其添加到您的问题中,请停止尝试将所有内容都放入评论中。 – greatwolf

+0

完成。我很抱歉,我是这个网站的新手,我仍然在想出如何正确地设置格式。 – MattDella

相关问题