Undefined reference to #39;vtable for xxx#39;(未定义对 vtable for xxx 的引用)
问题描述
我不断从链接器收到此错误,我知道它与临时存储 vtable 的内联函数有关.但这意味着什么我不太确定.我认为这与我如何在 takeaway.cpp 的初始化列表中调用 gameCore 的构造函数有关
I keep getting this Error from the linker , i know it has something to do with inline functions getting a vtable temporarily stored. But what that entails i am not quite sure. I would assume it has something to do with how i call gameCore's constructor in the initilization list of takeaway.cpp
我有一个模板类 (gameCore.h)和一个继承自gameCore的类(takeaway.cpp)vtable 错误被调用 3 次1)在外卖构造函数中2) 外卖析构函数3)在gameCores构造函数中
I have a templated class (gameCore.h) and a class (takeaway.cpp) that is inheriting from gameCore The vtable error is called 3 times 1)in takeaways constructor 2) takeaways destructor 3)in gameCores constructor
我正在使用 G++这是代码:(我知道它可能看起来很难阅读,但我已经明确地标出了错误发生的位置)外卖.h
I am using G++ Here is the code: (i know it may seem hard to read but i have marked off exatcly where the erros occur) takeaway.h
takeaway.cpp
takeaway.cpp
gameCore.h
推荐答案
第一组错误,对于丢失的vtable,是因为你没有实现takeaway::textualGame()
;相反,您实现了一个非成员函数 textualGame()
.我认为添加缺少的 takeaway::
会解决这个问题.
The first set of errors, for the missing vtable, are caused because you do not implement takeaway::textualGame()
; instead you implement a non-member function, textualGame()
. I think that adding the missing takeaway::
will fix that.
最后一个错误的原因是你从 gameCore
的构造函数中调用了一个虚函数 initialData()
.在这个阶段,根据当前正在构造的类型(gameCore
),不是最派生的类(takeaway
)来调度虚函数.这个特定的函数是纯虚函数,因此在这里调用它会产生未定义的行为.
The cause of the last error is that you're calling a virtual function, initialData()
, from the constructor of gameCore
. At this stage, virtual functions are dispatched according to the type currently being constructed (gameCore
), not the most derived class (takeaway
). This particular function is pure virtual, and so calling it here gives undefined behaviour.
两种可能的解决方案:
- 将
gameCore
的初始化代码移出构造函数并放入单独的初始化函数中,该函数必须在对象完全构造后调用;或 - 将
gameCore
分成两个类:一个由takeaway
实现的抽象接口,一个包含状态的具体类.先构造takeaway
,然后(通过接口类的引用)传递给具体类的构造函数.
- Move the initialisation code for
gameCore
out of the constructor and into a separate initialisation function, which must be called after the object is fully constructed; or - Separate
gameCore
into two classes: an abstract interface to be implemented bytakeaway
, and a concrete class containing the state. Constructtakeaway
first, and then pass it (via a reference to the interface class) to the constructor of the concrete class.
我会推荐第二种,因为它是向更小的类和更松散的耦合方向发展,并且更难错误地使用这些类.第一个更容易出错,因为无法确保初始化函数被正确调用.
I would recommend the second, as it is a move towards smaller classes and looser coupling, and it will be harder to use the classes incorrectly. The first is more error-prone, as there is no way be sure that the initialisation function is called correctly.
最后一点:基类的析构函数通常应该是虚拟的(以允许多态删除)或受保护的(以防止无效的多态删除).
One final point: the destructor of a base class should usually either be virtual (to allow polymorphic deletion) or protected (to prevent invalid polymorphic deletion).
这篇关于未定义对 'vtable for xxx' 的引用的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!