对“vtable”的未定义引用

Posted

技术标签:

【中文标题】对“vtable”的未定义引用【英文标题】:Undefined reference to "vtable" 【发布时间】:2015-07-05 10:14:47 【问题描述】:

这是我的程序。它有一个基类Point,一个继承自Point的类colored_point,以及一个继承自colored_point的类dim3_point。在 Point 类中有一个虚函数 Initializer(),在其他类中也有一个。

#include <stdio.h>
#include <conio.h>
#include <iostream>

#define BLACK 0

using namespace std;

class point

    private:
        float x,y;
    public:
        point();
        point(float ox , float oy );
         point(const point &p);
        ~point();
        void display();
        void move(float dx, float dy);
        virtual void Initializer()
        
            cout << "Diem khong mau hic hic" << endl;
        
;

class colored_point: public point

    private:
        unsigned int color;
    public:
        colored_point(): point()
        
            color = BLACK;
        
        colored_point(float ox , float oy , unsigned int Color = BLACK): point(ox,oy )
        
            cout << "Goi ham to mau " << endl;
            color = Color;
        
        colored_point(const colored_point &p_color);
        void Initializer();
        ~colored_point();
        //void display();
;

class dim3_point: public colored_point

    private:
        float z;
    public:
        dim3_point();
        dim3_point(float ox, float oy, float oz, unsigned int Color = BLACK);
        dim3_point(const dim3_point &p);
        ~dim3_point();
        void Initializer();
;

int main()

    point *atsm;
    colored_point P1(2,3,5);
    atsm = &P1;
    atsm->display();
    getch();
    return 0;


point::point()

    x = 0, y =0;


point::point(float ox  , float oy )

    cout << "Goi ham point::point" << endl;
    x = ox, y = oy;


point::point(const point &p)

    x = p.x, y = p.y;


point::~point()

    cout << "Burn baby burn !!" << endl;

void point::display()

    cout << "Toa do:" << x << "," << y << endl;
    Initializer();


void point::move(float dx, float dy)

    x += dx, y += dy;


//void point::Initializer()
//
//    cout << "Diem khong mau hic hic" << endl;
//

colored_point::colored_point(const colored_point &p): point::point((point&)p)
        
            color = p.color;
        

colored_point::~colored_point()




dim3_point::dim3_point(): colored_point::colored_point()

    z = 0;


dim3_point::dim3_point(float ox, float oy, float oz, unsigned int Color): colored_point::colored_point(ox, oy, Color)

    cout << "Goi ham 3D" << endl;
    z = oz;


dim3_point::dim3_point(const dim3_point &p): colored_point::colored_point((const colored_point&) p )



但是我收到以下错误:

||=== Build file: "no target" in "no project" (compiler: unknown) ===|
C:\Users\son\Documents\test2.o:test2.cpp|| undefined reference to `vtable for colored_point'|
C:\Users\son\Documents\test2.o:test2.cpp|| undefined reference to `vtable for colored_point'|
C:\Users\son\Documents\test2.o:test2.cpp|| undefined reference to `vtable for dim3_point'|
C:\Users\son\Documents\test2.o:test2.cpp|| undefined reference to `vtable for dim3_point'|
C:\Users\son\Documents\test2.o:test2.cpp|| undefined reference to `vtable for dim3_point'|
C:\Users\son\Documents\test2.o:test2.cpp:(.text$_ZN13colored_pointC2Ev[__ZN13colored_pointC2Ev]+0x18)||undefined reference to `vtable for colored_point'|
C:\Users\son\Documents\test2.o:test2.cpp:(.text$_ZN13colored_pointC2Effj[__ZN13colored_pointC2Effj]+0x5e)||undefined reference to `vtable for colored_point'|
C:\Users\son\Documents\test2.o:test2.cpp:(.text$_ZN13colored_pointC1Effj[__ZN13colored_pointC1Effj]+0x5e)||undefined reference to `vtable for colored_point'|
||error: ld returned 1 exit status|
||=== Build failed: 9 error(s), 0 warning(s) (0 minute(s), 1 second(s)) ===|

我知道还有其他类似的帖子,但到目前为止它们还没有为我工作。非常感谢!

【问题讨论】:

在类层次结构中创建所有析构函数virtual。另外请提供一个重现错误的minimal example,而不是在这里转储您的所有代码。 Undefined reference to vtable的可能重复 【参考方案1】:

通过 clang 抛出相同的代码示例(很好地注释掉 conio.h 和 getch())可以提供:

Undefined symbols for architecture x86_64:
  "vtable for dim3_point", referenced from:
      dim3_point::dim3_point() in stackex-7c554b.o
      dim3_point::dim3_point(float, float, float, unsigned int) in stackex-7c554b.o
      dim3_point::dim3_point(dim3_point const&) in stackex-7c554b.o
  NOTE: a missing vtable usually means the first non-inline virtual member function has no definition.
  "vtable for colored_point", referenced from:
      colored_point::colored_point(colored_point const&) in stackex-7c554b.o
      colored_point::colored_point() in stackex-7c554b.o
      colored_point::colored_point(float, float, unsigned int) in stackex-7c554b.o
  NOTE: a missing vtable usually means the first non-inline virtual member function has no definition.
ld: symbol(s) not found for architecture x86_64
clang: error: linker command failed with exit code 1 (use -v to see invocation)

注意这里clang提供的“NOTE”,然后检查你在类中定义的所有虚函数实际上都有实现。 (在这种情况下,Initialize 在两个子类中都没有实现,尽管它们的类定义中都有该函数的原型)。

这应该可以解决您的编译错误。但如上所述,如果你的类中有任何虚函数,你也应该将你的析构函数设为虚函数。

【讨论】:

以上是关于对“vtable”的未定义引用的主要内容,如果未能解决你的问题,请参考以下文章

使用 Qt 对 vtable 的未定义引用

gcc:架构 x86_64 的未定义符号,缺少 vtable

c++ 继承问题:未定义对“vtable”的引用

链接器错误:未定义对“QGLViewer 的 vtable”的引用

由于交叉编译树莓派,未定义对“Adafruit_GFX 的 vtable”的引用

error:对‘vtable for new_sequence’未定义的引用 对‘typeinfo for num_sequence’未定义的引用