Undefined reference to 'vtable for xxx'

后端 未结 6 694
野性不改
野性不改 2020-12-24 06:06
takeaway.o: In function `takeaway\':
project:145: undefined reference to `vtable for takeaway\'
project:145: undefined reference to `vtable for takeaway\'
takeaway.o         


        
6条回答
  •  再見小時候
    2020-12-24 06:22

    If a class defines virtual methods outside that class, then g++ generates the vtable only in the object file that contains the outside-of-class definition of the virtual method that was declared first:

    //test.h
    struct str
    {
       virtual void f();
       virtual void g();
    };
    
    //test1.cpp
    #include "test.h"
    void str::f(){}
    
    //test2.cpp
    #include "test.h"
    void str::g(){}
    

    The vtable will be in test1.o, but not in test2.o

    This is an optimisation g++ implements to avoid having to compile in-class-defined virtual methods that would get pulled in by the vtable.

    The link error you describe suggests that the definition of a virtual method (str::f in the example above) is missing in your project.

提交回复
热议问题