undefined reference to vtable for triangle

confused why I'm getting this error, I thought I had every virtual function in my ABC "shape.cpp" defined in triangle. I'm thinking it has something to do with my constructor in triangle (how I call shape's constructor?) but I don't know how to go about testing?

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
#ifndef _Shape_cpp_
#define _Shape_cpp_
//#include "Triangle.cpp"
#include <iostream>
using namespace std;

class Shape {
protected:
  double xCoord,yCoord;
public:
	Shape(double x = 0, double y = 0) : xCoord(x),yCoord(y){}
  	virtual double area() = 0;
  	virtual void draw() const = 0; 
  	virtual ~Shape(){} 
};

#endif


1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
#include "Shape.cpp"
#ifndef Triangle_cpp
#define Triangle_cpp

class Triangle : public Shape {
private:
double hypo;
    
public:
    Triangle(double x, double y, double hp) : Shape(x,y), hypo(hp){} 
  	double area(){return xCoord * yCoord/2;}
  	void draw() const{//omitted}
  	~Triangle();
};
#endif


1
2
3
4
5
6
7
8
9
10
11
12
#include "Shape.cpp"
#include "Triangle.cpp"
#include "Picture.cpp"
#include <iostream>
using namespace std;

int main(){
    Picture p;
    p.insert(new Triangle(5,5,5));
    return 0;
}


edit: forgot context, I'm trying to derive a bunch of child classes from an abstract base class 'shape'. Then I'm going to create a class called picture that uses a link list to store a list of shapes. I've no compile errors that refer to picture being the problem when I run my main - it's just my triangle.

edit2: /tmp/ccI1iTRS.o: In function `Triangle::Triangle(double, double, double)':
shapemain.cpp:(.text._ZN8TriangleC2Eddd[_ZN8TriangleC5Eddd]+0x48): undefined reference to `vtable for Triangle'
collect2: error: ld returned 1 exit status
Last edited on
Where is the destructor of Triangle defined?
Oh... I thought I should have left it undefined so that the ABC's destructor is called instead and automatically destroys all derived instances of that class. Now that I have a {} instead of a ; there, will I have to destruct all derived instances of the shape class individually? I won't get to destroy them all at once using shape's destructor, right?
When a Triangle is destroyed ~Triangle() and ~Shape() will run, in that order.
Topic archived. No new replies allowed.