C++ Help: gcc error undefined reference to `vtable for Triangle'

xSnowmaNx

Gawd
Joined
Aug 11, 2004
Messages
708
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:

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];
}
 
Having the command line you executed would be nice, but I am going to go out on a limb and guess you did something like:

Code:
make lab9

or:

Code:
g++ lab9.C -o lab9

How does the linker know to get the infomation out of Triangle.C?

Hopefully that will get you to your next set of really confusing errors ;)
 
Well, I compile the Triangle.C to object code:

g++ Triangle.C -c

then I simply use your second command:

g++ lab9.C -o lab9

When I try to compile lab9.C is when I get these errors.
 
Back
Top