Warning: file_get_contents(/data/phpspider/zhask/data//catemap/6/cplusplus/128.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C++ 未定义的引用;vtable“;_C++_Inheritance_Virtual Functions_Vtable - Fatal编程技术网

C++ 未定义的引用;vtable“;

C++ 未定义的引用;vtable“;,c++,inheritance,virtual-functions,vtable,C++,Inheritance,Virtual Functions,Vtable,这是我的节目。它有一个基类Point、一个从Point继承的类Color_Point和一个从Color_Point继承的类dim3_Point。在类点中有一个虚拟函数初始值设定项(),在其他类中也有一个 #include <stdio.h> #include <conio.h> #include <iostream> #define BLACK 0 using namespace std; class point { private:

这是我的节目。它有一个基类Point、一个从Point继承的类Color_Point和一个从Color_Point继承的类dim3_Point。在类点中有一个虚拟函数初始值设定项(),在其他类中也有一个

#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 )
{

}

我知道还有其他类似的帖子,但到目前为止,它们还没有为我工作。多谢各位

通过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在两个子类中都缺少实现,即使它们的类定义中都有该函数的原型)


这将修复编译错误。但是如上所述,如果类中有任何虚拟函数,也应该将析构函数设置为虚拟。

将类层次结构中的所有析构函数设置为虚拟。另外,请提供一个复制错误的方法,而不是在此处转储所有代码
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)