所以,我变得臭名昭着
未定义的引用'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 …Run Code Online (Sandbox Code Playgroud) 我尝试学习 C++ 构造函数,我还是菜鸟。我写了下一堂课:
screen.h
#ifndef SCREEN_H
#define SCREEN_H
#include "pch.h"
class Screen
{
public:
Screen(const std::string& name);
Screen(const Screen& screen);
Screen(Screen&& screen);
const std::string& name() const;
virtual void draw();
private:
std::string m_name;
};
#endif // SCREEN_H
Run Code Online (Sandbox Code Playgroud)
屏幕.cpp
#include "screen.h"
Screen::Screen(const std::string& name)
: m_name{name}
{
m_name = name;
}
Screen::Screen(const Screen& screen)
: m_name{screen.m_name}
{
}
Screen::Screen(Screen&& screen)
: m_name{std::move(screen.m_name)}
{
}
const std::string& Screen::name() const
{
return this->m_name;
}
Run Code Online (Sandbox Code Playgroud)
但我在编译时遇到一个问题:
screen.cpp:4: 错误:未定义对“屏幕 vtable”的引用
对于所有构造函数也是如此。
我不明白这是我的错误...请问有人可以解释我吗?
我有一个声明如下的类:
class TestFoo {
public:
TestFoo();
virtual void virtualFunction();
void nonVirtualFunction();
};
Run Code Online (Sandbox Code Playgroud)
我尝试以这种方式实现
TestFoo::TestFoo(){}
void TestFoo::nonVirtualFunction(){}
Run Code Online (Sandbox Code Playgroud)
在编译时返回错误:
undefined reference to vtable for TestFoo
Run Code Online (Sandbox Code Playgroud)
我试过了 :
TestFoo::TestFoo(){}
void TestFoo::nonVirtualFunction(){}
void TestFoo::virtualFunction(){}
Run Code Online (Sandbox Code Playgroud)
编译确定与这些帖子的答案一致:
令我困惑的是,我认为声明虚函数的重点在于我不需要定义它.在这个例子中,我不打算创建任何TestFoo实例,而是创建从TestFoo继承的(具体)类的实例.但是,我仍然想为TestFoo的每个子类定义函数nonVirtualFunction.
我不对劲的东西?
谢谢 !