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!