1

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.

7
  • Try 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. Commented Jun 28, 2022 at 11:36
  • gcc seems to want to put the vtable in the object file where the first virtual function is implemented. Don't know what happens when there is no object file. Perhaps this? Commented Jun 28, 2022 at 11:41
  • I don't see anything wrong with the shown code. Please form a minimal reproducible example. Commented Jun 28, 2022 at 11:54
  • Some compilers use the strategy of generating the virtual function table into the object file by the source file translation unit that implements the first non-inline virtual function. Commented Jun 28, 2022 at 12:09
  • @Quimby: I tried putting in minimal reproducible code. But its working in that case by virtual ~ActiveState()=default;, but not in the actual case. Not sure how to put forward as I cannot share the complete code and whatever I have shared earlier seems to have problem. Commented Jun 28, 2022 at 12:36

1 Answer 1

1

Sorry guys, It was a problem with CMakeLists.txt. The CMakeLists.txt was missing ActiveState.cpp in GLOB. Hence this error. Thanks for your help.

Sign up to request clarification or add additional context in comments.

1 Comment

That just happens sometimes :D

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.