I'm currently working on a class project that uses dynamic binding and virtual functions to allow a user to create shapes and then call upon those objects to calculate the areas for each shape the user created and print out the information.
When I try to compile what I have, gcc spits out:
I have never encountered this error before and my teacher never mentioned this as something to look out for, so I have no idea what this is telling me. I've searched on Google for some possible explanations, but the results I found were vague at best. They seem to indicate that gcc is finicky with declaration/definitions of virtual functions? Others say there might be something up with constructors/destructors of the objects. I really don't know where to go with this one.
Anyways, my files currently look like this:
Shape.h:
Triangle.h:
Triangle.C:
and the main portion, lab9.C:
When I try to compile what I have, gcc spits out:
Code:
/tmp/ccO3xwxo.o: In function `Triangle::Triangle()':
lab9.C:(.text._ZN8TriangleC1Ev[Triangle::Triangle()]+0x22): undefined reference to `vtable for Triangle'
I have never encountered this error before and my teacher never mentioned this as something to look out for, so I have no idea what this is telling me. I've searched on Google for some possible explanations, but the results I found were vague at best. They seem to indicate that gcc is finicky with declaration/definitions of virtual functions? Others say there might be something up with constructors/destructors of the objects. I really don't know where to go with this one.
Anyways, my files currently look like this:
Shape.h:
Code:
#ifndef _SHAPE_H_
#define _SHAPE_H_
#include <iostream>
using namespace std;
class Shape
{
protected:
float area;
float perimeter;
int sides;
public:
//Overloaded output stream
friend ostream &operator<<( ostream &strm, Shape &a )
{
return a.Print( strm );
}
//Virtual print
virtual ostream &Print( ostream & ) = 0;
//Constructor
Shape(float a = 0.0, float b = 0.0) //, int c = 0)
{
area = a;
perimeter = b;
//sides = c;
}
/*Access methods
void setArea(float);
void setPerimeter(float);
void setSides(int);
float getArea();
float getPerimeter();
int getSides();*/
//Virtual area calculation function
virtual void Area() = 0;
};
#endif
Triangle.h:
Code:
#ifndef _TRIANGLE_H_
#define _TRIANGLE_H_
#include "Shape.h"
class Triangle : public Shape
{
protected:
float base;
float height;
public:
// Constructor
Triangle(float a, float b) //, float c = 0.0)
{
//area = a;
base = a;
height = b;
sides = 3;
}
Triangle()
{
base = 0;
height = 0;
}
// Dynamically bound functions
virtual void Area();
virtual ostream &Print( ostream & );
// Access functions
void setBase(float);
void setHeight(float);
float getBase();
float getHeight();
};
#endif
Triangle.C:
Code:
#include "Triangle.h"
void Triangle::Area()
{
area = (base * height) / 2;
}
ostream &Triangle::Print( ostream &strm )
{
strm << "Triangle:\n";
strm << " Base: " << base;
strm << " Height: " << height;
strm << " Area: " << area;
return strm;
}
void Triangle::setBase(float a)
{
base = a;
}
void Triangle::setHeight(float a)
{
height = a;
}
float Triangle::getHeight()
{
return height;
}
float Triangle::getBase()
{
return base;
}
and the main portion, lab9.C:
Code:
#include <vector>
#include "Shape.h"
//#include "Circle.h"
//#include "Rectangle.h"
#include "Triangle.h"
main()
{
vector<Shape *> shapes;
shapes.push_back(new Triangle());
// cout << shapes[0];
}