-2

Here is my program. It has a base class, Point, a class colored_point inherited from Point, and a class dim3_point inherited from colored_point. In the class Point there is a virtual function Initializer() and there is one in other classes as well.

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

}

However I received the following errors:

||=== 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)) ===|

I know that there has been other similar posts about this, but so far they haven't worked for me. Thank you very much!

tangrs
  • 9,709
  • 1
  • 38
  • 53
Dang Manh Truong
  • 795
  • 2
  • 10
  • 35
  • Make all of your destructors `virtual` in the class hierarchy. Also please provide a [minimal example](http://stackoverflow.com/help/mcve) that reproduces the error, instead of just dumping all of your code here. – πάντα ῥεῖ Jul 05 '15 at 10:17
  • Possible duplicate of [Undefined reference to vtable](http://stackoverflow.com/questions/3065154/undefined-reference-to-vtable?rq=1) – πάντα ῥεῖ Jul 05 '15 at 10:23

1 Answers1

0

Throwing the same code example (well commenting out conio.h and getch()) through clang provides this:

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)

Note the "NOTE" provided by clang here, and then check that all the virtual functions you have defined in the class actually have implementations. (In this case, Initialize lacks an implementation in both sub-classes, even though they both have a prototype for that function in their class definition).

That should fix your compile error. But as stated above, you should also make your destructor virtual if you have any virtual functions in your class.

πάντα ῥεῖ
  • 1
  • 13
  • 116
  • 190
somaen
  • 21
  • 2