Undefined reference to vtable

前端 未结 16 2141
死守一世寂寞
死守一世寂寞 2020-11-21 20:30

When building my C++ program, I\'m getting the error message

undefined reference to \'vtable...

What is the cause of this probl

16条回答
  •  难免孤独
    2020-11-21 21:28

    Undefined reference to vtable may occur due to the following situation also. Just try this:

    Class A Contains:

    virtual void functionA(parameters)=0; 
    virtual void functionB(parameters);
    

    Class B Contains:

    1. The definition for the above functionA.
    2. The definition for the above functionB.

    Class C Contains: Now you're writing a Class C in which you are going to derive it from Class A.

    Now if you try to compile you will get Undefined reference to vtable for Class C as error.

    Reason:

    functionA is defined as pure virtual and its definition is provided in Class B. functionB is defined as virtual (NOT PURE VIRTUAL) so it tries to find its definition in Class A itself but you provided its definition in Class B.

    Solution:

    1. Make function B as pure virtual (if you have requirement like that) virtual void functionB(parameters) =0; (This works it is Tested)
    2. Provide Definition for functionB in Class A itself keeping it as virtual . (Hope it works as I didn't try this)

提交回复
热议问题