Hello i am trying to create a simple program in C++ and I have problems in linking the files. The error is undefined reference to my copy constructors in the classes. I checked the linking and I can't find any errors in the header file.
I tried messing around with the copy constructors but still no solution. when i comment the copy constructors, the program compiles.
Error message:
/tmp/cc5KI4Dx.o: In function `CenteredShape::CenteredShape(CenteredShape const&)':
Shapes.cpp:(.text+0x1c0): undefined reference to `Shape::Shape()'
/tmp/cc5KI4Dx.o: In function `RegularPolygon::RegularPolygon(RegularPolygon const&)':
Shapes.cpp:(.text+0x258): undefined reference to `CenteredShape::CenteredShape()'
/tmp/cc5KI4Dx.o: In function `Circle::Circle(Circle const&)':
Shapes.cpp:(.text+0x388): undefined reference to `CenteredShape::CenteredShape()'
/tmp/cc5KI4Dx.o: In function `Rectangle::Rectangle(Rectangle const&)':
Shapes.cpp:(.text+0x42c): undefined reference to `RegularPolygon::RegularPolygon()'
/tmp/cc5KI4Dx.o: In function `Square::Square(Square const&)':
Shapes.cpp:(.text+0x576): undefined reference to `RegularPolygon::RegularPolygon()'
collect2: error: ld returned 1 exit status
testshapes.cpp
#include "Shapes.h"
int main(int argc, char** argv) {
Circle c("first circle", 3, 4, 7);
RegularPolygon r("TRIANGLE", 1, 1, 3);
r.printName();
c.printName();
}
Shapes.h
/*
Classic shape examples: an inheritance tree in a geometric context
*/
#ifndef __SHAPES_H
#define __SHAPES_H
#include <string>
class Shape { // base class
private: // private access modifier: could also be protected
std::string name; // every shape will have a name
public:
Shape(const std::string&); // builds a shape with a name
Shape(); // empty shape
Shape(const Shape&);
// copy constructor
void printName() const ; // prints the name
//setters
void setName(std::string newName);
//getters
std::string getName();
};
class CenteredShape : public Shape { // inherits from Shape
private:
double x,y; // the center of the shape
public:
CenteredShape(const std::string&, double, double); // usual three constructors
CenteredShape();
CenteredShape(const CenteredShape&);
//setters
void move(double, double); // moves the shape, i.e. it modifies it center
//getters
double getX();
double getY();
};
class RegularPolygon : public CenteredShape { // a regular polygon is a centered_shape with a number of edges
private:
int EdgesNumber;
public:
RegularPolygon(const std::string&, double, double, int);
RegularPolygon();
RegularPolygon(const RegularPolygon&);
//setters
void setEdgesNumber(int newEdgesNumber);
//getter
int getEdgesNumber();
};
class Circle : public CenteredShape { // a Circle is a shape with a center and a radius
private:
double Radius;
public:
Circle(const std::string&, double, double, double);
Circle();
Circle(const Circle&);
//methods
double perimeter();
double area();
//setters
void setRadius(double newRadius);
//getters
double getRadius();
};
class Rectangle : public RegularPolygon { // a Rectange is a shape with edges
private:
double length;
double width;
public:
Rectangle(const std::string& n, double nx, double ny, double nwidth,double nheight);
Rectangle();
Rectangle(const Rectangle&);
//methods
double perimeter();
double area();
//setters
void setLength(double newLength);
void setWidth(double newWidth);
//getters
double getLength();
double getWidth();
};
class Square : public RegularPolygon { // a Square is a shape with edges
private:
double side;
public:
Square(const std::string& n, double nx, double ny, double nside);
Square();
Square(const Square&);
//methods
double perimeter();
double area();
//setters
void setSide(double newSide);
//getters
double getSide();
};
#endif
Shapes.cpp
// please refer to shapes.h for methods documentation
#include <iostream>
#include "Shapes.h"
using namespace std;
//------------------------------------------------------
Shape::Shape(const string& n) : name(n) {
}
// copy constructor
Shape::Shape(const Shape &shp)
{
name = shp.name;
}
void Shape::printName() const {
cout << name << endl;
}
void Shape::setName(string newName)
{
name = newName;
}
string Shape::getName()
{
return name;
}
//---------------------------------------------------------
void CenteredShape::move(double nx, double ny)
{
x=nx;
y=ny;
}
double CenteredShape::getX()
{
return x;
}
double CenteredShape::getY()
{
return y;
}
CenteredShape::CenteredShape(const string &n, double nx, double ny): Shape(n) {
x = nx;
y = ny;
}
CenteredShape::CenteredShape(const CenteredShape &cshp)
{
x=cshp.x;
y=cshp.y;
}
//-----------------------------------------------------------------------------------
RegularPolygon::RegularPolygon(const string& n, double nx, double ny, int nl) : CenteredShape(n,nx,ny)
{
EdgesNumber = nl;
}
RegularPolygon::RegularPolygon(const RegularPolygon ®p)
{
EdgesNumber=regp.EdgesNumber;
}
void RegularPolygon::setEdgesNumber(int newEdgesNumber)
{
EdgesNumber=newEdgesNumber;
}
int RegularPolygon::getEdgesNumber()
{
return EdgesNumber;
}
//----------------------------------------------------------------------------------
double Circle::perimeter()
{
return (2*3.14*Radius);
}
double Circle::area()
{
return(3.14*Radius*Radius);
}
void Circle::setRadius(double newRadius)
{
Radius=newRadius;
}
double Circle::getRadius()
{
return Radius;
}
Circle::Circle(const string& n, double nx, double ny, double r) : CenteredShape(n,nx,ny)
{
Radius = r;
}
Circle::Circle(const Circle &cr)
{
Radius=cr.Radius;
}
//------------------------------------------------------------------------------------
Rectangle::Rectangle(const string &n, double nx, double ny, double nwidth, double nheight) : RegularPolygon(n,nx,ny,4)
{
length=nheight;
width=nwidth;
}
Rectangle::Rectangle(const Rectangle &rect)
{
length=rect.length;
width=rect.width;
}
double Rectangle::perimeter()
{
return (2*(length+width));
}
double Rectangle::area()
{
return (length*width);
}
void Rectangle::setLength(double newLength)
{
length = newLength;
}
void Rectangle::setWidth(double newWidth)
{
width = newWidth;
}
double Rectangle::getLength()
{
return length;
}
double Rectangle::getWidth()
{
return width;
}
//------------------------------------------------------------------------------
Square::Square(const string& n, double nx, double ny, double nside):RegularPolygon(n,nx,ny,4)
{
side=nside;
}
Square::Square(const Square &sqr)
{
side = sqr.side;
}
double Square::perimeter()
{
return (side*4);
}
double Square::area()
{
return (side*side);
}
void Square::setSide(double newSide)
{
side=newSide;
}
double Square::getSide()
{
return side;
}