I have this error coming up:
undefined reference to
vtable
I'm unable to figure out the root cause of it.
I did try to search in Google, but still unable to figure out the reason.
Here is the code:
IState.hpp -> Complete state interface class
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 -> Partial implementation of interface class
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 -> Complete implementation of 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;
};
};
Now, whenever I create an ActiveState object using a base class pointer, it goes through compilation, but fails in linking with this error:
In function `ActiveState::ActiveState(ICallbacks&)':
File.cpp:(.text.11ActiveStateC2ERNS2_15ICallbacksE[_ZN611ActiveStateC5ERNS2_15ICallbacksE]+0x26): undefined reference to `vtable for ActiveState'
Please let me know where I am going wrong, and how to solve this problem.
virtual ~ActiveState()=default;or put the implementation in the .cpp file. If that does not work, create a minimal reproducible example, this is a linker error yet I see no translation units in your question, headers are almost irrelevant to this.