I am having difficulties with compiling the these two programs together. I get no errors when compiling the function file and the main into .o object files but when compiling together I get undefined reference to the constructors and functions. Why is this, what am I missing? Thanks!
#include "shape.h" //header file
usingnamespace std;
int main() {
//Pointer Array--------------------------------------------
Shape* arr[6]; //Assign Shape Dimensions and to Array
Circle cir(2); //declares value for circle ~ cir is var name
arr[0] = ○ //assigns cir var to array position 0
Square sqr(3);
arr[1] = &sqr;
Triangle tri(4, 2);
arr[2] = &tri;
Sphere sph(5);
arr[3] = &sph;
Cube cb(6);
arr[4] = &cb;
Tetrahedron trhd(7);
arr[5] = &trhd;
//Loop each index of array and perform calculation
for (int i = 0; i < 6; ++i) {
cout << "\n Shape Name: \n" << arr[i]->getName()
<< " with area = " << arr[i]->getArea();
if ( arr[i]->get_NumberDimensions() == 3 )
{
cout <<"\n with volume = " << arr[i]->getVolume();
}
}
}
undefined reference to `Shape::getArea()'
undefined reference to `Shape::getName()'
undefined reference to `Shape::getVolume()'
undefined reference to `Shape::~Shape()'
undefined reference to `ThreeDimensionalshape::ThreeDimensionalshape()'
undefined reference to `TwoDimensionalShape::TwoDimensionalShape()'
get{Area,Name,Volume}() are virtual functions. If you do not intend to provide them a body, mark them as pure virtual virtualdouble getArea() = 0;
you never defined the destructor for Shape. An empty one may suffice. Given that you've got virtual member functions, you ougth to mark the destructor as virtual too. virtual ~Shape() = default;
You didn't provide a definition for the constructor of {Three,Two}DimensionalShape, ¿how the hell do you expect that to work?
So now declaring the virtuals as pure I get another error stating abstract type. Also how should I provide a constructor for TwoDimensionalShape and ThreeDimensionalShape? Isn't TwoDimensionalShape(); the default constructor?
main.cpp:13:9: error: cannot declare variable ‘cir’ to be of abstract type ‘Circle’
Circle cir(2); //declares value for circle ~ cir is var name
^
In file included from main.cpp:6:0:
shape.h:55:7: note: because the following virtual functions are pure within ‘Circle’:
class Circle : public TwoDimensionalShape {
^
shape.h:38:18: note: virtualdouble Shape::getVolume()
virtualdouble getVolume() = 0;
^
main.cpp:15:9: error: cannot declare variable ‘sqr’ to be of abstract type ‘Square’
Square sqr(3);
^
In file included from main.cpp:6:0:
shape.h:62:7: note: because the following virtual functions are pure within ‘Square’:
class Square : public TwoDimensionalShape {
^
shape.h:38:18: note: virtualdouble Shape::getVolume()
virtualdouble getVolume() = 0;
^
main.cpp:17:11: error: cannot declare variable ‘tri’ to be of abstract type ‘Triangle’
Triangle tri(4, 2);
^
In file included from main.cpp:6:0:
shape.h:69:7: note: because the following virtual functions are pure within ‘Triangle’:
class Triangle : public TwoDimensionalShape {
^
shape.h:38:18: note: virtualdouble Shape::getVolume()
virtualdouble getVolume() = 0;
> Isn't TwoDimensionalShape(); the default constructor?
If you don't declare any constructors, the compiler will provide default constructor for you.
If you declare a constructor that takes no parameters (e.g., TwoDimensionalShape();) then you need to provide a definition.
If the default behaviour was good enough for you, then you can mark it as such
1 2 3 4
class TwoDimensionalShape : public Shape {
public:
TwoDimensionalShape() = default;
};
> error: cannot declare variable ‘cir’ to be of abstract type ‘Circle’
> because the following virtual functions are pure within ‘Circle’:
> virtualdouble getVolume() = 0;
you never defined how the volume of a circle/sqare/triangle should be computed.
1 2 3 4 5 6 7
class TwoDimensionalShape : public Shape {
public:
TwoDimensionalShape() = default;
virtualdouble getVolume() override{
return 0;
}
};