未明确引用" vtable"

时间:2015-07-05 10:14:47

标签: c++ inheritance virtual-functions vtable

这是我的计划。它有一个基类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)) ===|

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

1 个答案:

答案 0 :(得分:0)

通过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)

注意&#34;注意&#34;由clang提供,然后检查您在类中定义的所有虚函数是否实际具有实现。 (在这种情况下,Initialize在两个子类中都缺少一个实现,即使它们在类定义中都有该函数的原型)。

那应该修复你的编译错误。但如上所述,如果你的班级中有任何虚函数,你也应该让你的析构函数为虚拟。