在构建C++程序时,我收到了错误消息
对“vtable”的引用未定义。。。
造成这个问题的原因是什么? 我该怎么修?
碰巧,我得到了以下代码的错误(所讨论的类是CGameModule) 而我无论如何也不明白问题出在哪里。 起初,我以为这与忘记给一个虚函数一个形体有关,但据我理解,一切都在这里。 继承链有点长,但这里是相关的源代码。 我不确定我还应该提供什么信息。
注意:构造函数似乎是发生错误的地方。
我的代码:
class CGameModule : public CDasherModule {
public:
CGameModule(Dasher::CEventHandler *pEventHandler, CSettingsStore *pSettingsStore, CDasherInterfaceBase *pInterface, ModuleID_t iID, const char *szName)
: CDasherModule(pEventHandler, pSettingsStore, iID, 0, szName)
{
g_pLogger->Log("Inside game module constructor");
m_pInterface = pInterface;
}
virtual ~CGameModule() {};
std::string GetTypedTarget();
std::string GetUntypedTarget();
bool DecorateView(CDasherView *pView) {
//g_pLogger->Log("Decorating the view");
return false;
}
void SetDasherModel(CDasherModel *pModel) { m_pModel = pModel; }
virtual void HandleEvent(Dasher::CEvent *pEvent);
private:
CDasherNode *pLastTypedNode;
CDasherNode *pNextTargetNode;
std::string m_sTargetString;
size_t m_stCurrentStringPos;
CDasherModel *m_pModel;
CDasherInterfaceBase *m_pInterface;
};
继承自。。。
class CDasherModule;
typedef std::vector<CDasherModule*>::size_type ModuleID_t;
/// \ingroup Core
/// @{
class CDasherModule : public Dasher::CDasherComponent {
public:
CDasherModule(Dasher::CEventHandler * pEventHandler, CSettingsStore * pSettingsStore, ModuleID_t iID, int iType, const char *szName);
virtual ModuleID_t GetID();
virtual void SetID(ModuleID_t);
virtual int GetType();
virtual const char *GetName();
virtual bool GetSettings(SModuleSettings **pSettings, int *iCount) {
return false;
};
private:
ModuleID_t m_iID;
int m_iType;
const char *m_szName;
};
它继承自。。。。
namespace Dasher {
class CEvent;
class CEventHandler;
class CDasherComponent;
};
/// \ingroup Core
/// @{
class Dasher::CDasherComponent {
public:
CDasherComponent(Dasher::CEventHandler* pEventHandler, CSettingsStore* pSettingsStore);
virtual ~CDasherComponent();
void InsertEvent(Dasher::CEvent * pEvent);
virtual void HandleEvent(Dasher::CEvent * pEvent) {};
bool GetBoolParameter(int iParameter) const;
void SetBoolParameter(int iParameter, bool bValue) const;
long GetLongParameter(int iParameter) const;
void SetLongParameter(int iParameter, long lValue) const;
std::string GetStringParameter(int iParameter) const;
void SetStringParameter(int iParameter, const std::string & sValue) const;
ParameterType GetParameterType(int iParameter) const;
std::string GetParameterName(int iParameter) const;
protected:
Dasher::CEventHandler *m_pEventHandler;
CSettingsStore *m_pSettingsStore;
};
/// @}
#endif
GCC FAQ中有一个条目:
解决方案是确保定义所有不纯的虚拟方法。 注意,析构函数必须被定义,即使它被声明为pure-virtual[class.dtor]/7。
因此,您需要为虚拟析构函数提供一个定义:
virtual ~CDasherModule()
{ };
无论如何,在虚拟析构函数上忘记一个主体会生成以下结果:
未定义对“VTable for CyourClass”得引用。
我正在添加一个注释,因为错误消息是欺骗性的。 (这是gcc版本4.6.3.)
所以,我已经搞清楚了这个问题,它是一个糟糕的逻辑和不完全熟悉汽车/自动工具世界的组合。 我正在向makefile.am模板添加正确的文件,但我不确定构建过程中的哪一步实际上创建了makefile本身。 所以,我用一个旧的makefile编译,它根本不知道我的新文件。
感谢回复和GCC常见问题的链接。 我将确保阅读这是为了避免这个问题发生的真正原因。