Sunday 17 December 2017

c++ - Undefined reference to vtable while implementing virtual functions

I try to implement examples of "C++ An Introduction To
Programming" by Jesse Liberty and Tim Keogh but on implementing pure virtual functions
is compiling but not building. It is giving the error : undefined reference to 'vtable
for circle'



I have tried substituting the
variable itsRadius for a virtual function
GetItsRadius to see if it would work but it started giving me
the same errors in the switch statement but for Rectangle and
Square and also the same error as before for
circle.




Here
is the code:



#include


using namespace std;

enum
BOOL { FALSE, TRUE };

class Shape
{


public:
Shape(){}
~Shape(){}
virtual long GetArea() =
0; // error
virtual long GetPerim()= 0;
virtual void Draw() =
0;
private:
};

void
Shape::Draw()

{
cout << "Abstract drawing
mechanism!\n";
}

class Circle : public
Shape
{
public:
Circle(int
radius):itsRadius(radius){}
~Circle(){}
long GetArea() { return 3
* itsRadius * itsRadius; }

long GetPerim() { return 9 * itsRadius;
}
void Draw();
private:
int itsRadius;
int
itsCircumference;
};

class Rectangle : public
Shape
{
public:

Rectangle(int len, int
width):
itsLength(len), itsWidth(width){}

~Rectangle(){}
long GetArea() { return itsLength * itsWidth; }

long GetPerim() { return 2*itsLength + 2*itsWidth; }
virtual int GetLength()
{ itsLength; }
virtual int GetWidth() { itsWidth; }
void
Draw();
private:
int itsWidth;

int
itsLength;
};

void
Rectangle::Draw()
{
for (int i = 0; i
{
for (int j = 0; j cout << "x
";


cout << "\n";
}

Shape::Draw();
}

class Square : public
Rectangle
{
public:
Square(int len);

Square(int len, int width);

~Square(){}
long GetPerim()
{return 4 * GetLength();}
};

Square::Square(int
len):
Rectangle(len,len)
{}

Square::Square(int
len, int width):
Rectangle(len,width)

{
if
(GetLength() != GetWidth())
cout << "Error, not a square... a
Rectangle??\n";
}

void startof()
{

int choice;
BOOL fQuit = FALSE;
Shape *
sp;


while (1)
{
cout <<
"(1)Circle (2)Rectangle (3)Square (0)Quit: \n";
cin >>
choice;

switch (choice)
{
case 1: sp = new
Circle(5);
break;

case 2: sp = new
Rectangle(4,6);
break;
case 3: sp = new Square (5);

break;
default: fQuit = TRUE;
break;
}
if
(fQuit)
break;


sp->Draw();

cout << "\n";
}
}


int
main()
{

startof();
}

No comments:

Post a Comment

php - file_get_contents shows unexpected output while reading a file

I want to output an inline jpg image as a base64 encoded string, however when I do this : $contents = file_get_contents($filename); print &q...