提问人:TechTotie 提问时间:6/28/2022 最后编辑:Remy LebeauTechTotie 更新时间:6/29/2022 访问量:455
参数化构造函数出现“对'vtable'的未定义引用”错误
Getting "undefined reference to `vtable`" error for parameterized constructor
问:
我出现了这个错误:
未定义对
vtable
我无法找出它的根本原因。
我确实尝试在谷歌上搜索,但仍然无法找出原因。
代码如下:
IState.hpp -> 完整的状态接口类
class IState {
public:
IState() = delete;
IState(ICallbacks &callbacks)
: m_callbacks(callbacks) {
}
virtual ~IState() = default;
IState(IState &state)
:m_callbacks(state.m_callbacks) {
};
IState& operator=(IState &state) {
this->m_callbacks = state.m_callbacks;
return *this;
};
IState(IState &&state) = delete;
IState& operator=(IState &&state) = delete;
bool operator== (const IState &state) {
return (this->state_id == state.state_id);
}
virtual const IError& stateEntry(ISubject &Subject) = 0;
virtual const IError& stateExit(ISubject &Subject) = 0;
private:
std::string state_id = std::string("");
ICallbacks& m_callbacks;
};
CState.hpp -> 接口类的部分实现
class CState : public IState {
public:
CState() = delete;
CState(ICallbacks &callbacks)
: IState(callbacks),
m_ccallbacks(static_cast<CCallbacks&>(callbacks)) {
}
virtual ~CState() {
};
CState(CState &state)
:IState(state),
m_ccallbacks(static_cast<CCallbacks&>(state.m_ccallbacks)) {
}
CState& operator=(CState &state) {
this->m_ccallbacks = static_cast<CCallbacks&>(state.m_ccallbacks);
return *this;
}
CState(CState &&state) = delete;
CState& operator=(CState &&state) = delete;
const IError& stateEntry(ISubject &Subject) override {
CSubject &cSubject = static_cast<CSubject&>(Subject);
return StateEntry(cSubject);
};
const IError& stateExit(ISubject &Subject) override {
CSubject &cSubject = static_cast<CSubject&>(Subject);
return StateExit(cSubject);
}
virtual const IError& StateEntry(CSubject &Subject) = 0;
virtual const IError& StateExit(CSubject &Subject) = 0;
private:
CCallbacks& m_ccallbacks;
};
ActiveState.hpp -> 完整实现CState.hpp
class ActiveState : public CState {
public:
ActiveState() = delete;
ActiveState(ICallbacks &callbacks)
: CState(callbacks) {
}
ActiveState(ActiveState &state)
: CState(state) {
}
ActiveState& operator=(ActiveState &state) = default;
virtual ~ActiveState() {
}
const IError& StateEntry(CSubject &Subject) override {
return NO_ERROR;
};
const IError& StateExit(CSubject &Subject) override {
return NO_ERROR;
};
};
现在,每当我使用基类指针创建对象时,它都会进行编译,但链接失败并出现以下错误:ActiveState
In function `ActiveState::ActiveState(ICallbacks&)':
File.cpp:(.text.11ActiveStateC2ERNS2_15ICallbacksE[_ZN611ActiveStateC5ERNS2_15ICallbacksE]+0x26): undefined reference to `vtable for ActiveState'
请让我知道我哪里出了问题,以及如何解决这个问题。
答:
1赞
TechTotie
6/28/2022
#1
对不起,伙计们, 这是CMakeLists.txt的问题。 CMakeLists.txt 在 GLOB 中缺少 ActiveState.cpp。 因此出现此错误。 感谢您的帮助。
评论
0赞
Quimby
6/28/2022
这种情况有时会发生:D
评论
virtual ~ActiveState()=default;