Undefined reference to vtable

前端 未结 16 2142
死守一世寂寞
死守一世寂寞 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:32

    The GNU C++ compiler has to make a decision where to put the vtable in case you have the definition of the virtual functions of an object spread across multiple compilations units (e.g. some of the objects virtual functions definitions are in a .cpp file others in another .cpp file, and so on).

    The compiler chooses to put the vtable in the same place as where the first declared virtual function is defined.

    Now if you for some reason forgot to provide a definition for that first virtual function declared in the object (or mistakenly forgot to add the compiled object at linking phase), you will get this error.

    As a side effect, please note that only for this particular virtual function you won't get the traditional linker error like you are missing function foo.

提交回复
热议问题