Getting undefined refrence in the copy constructors C++ - c++

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 &regp)
{
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;
}

Related

How to fix class function "prototype does not match" and "cadidate is" errors

I'm desperately trying to finish this last assignment, and I'm at a complete loss for what these errors are trying to tell me to do. The errors are as such:
"prototype for 'double Rectangle::calculateArea()' does not match any in class 'Rectangle'" (line 40)
"candidate is: int Rectangle::calculateArea()" (line 11)
"prototype for 'double Rectangle::calculatePerimeter()' does not match any in class 'Rectangle'" (line 45)
"candidate is: int Rectangle::calculatePerimeter()" (line 12)
This is my first post on this forum, so I apologize in advance for it being ill-formatted
I haven't tried anything because none of the solutions I've found on forums relate directly to my problem (or so I think).
Thank you
// Rectangle.cpp
using namespace std;
class Rectangle
{
public:
void setLength(double length);
void setWidth(double width);
double getLength();
double getWidth();
int calculateArea() {return width*length;}
int calculatePerimeter() {return (width*2) + (length*2);}
private:
double length;
double width;
};
void Rectangle::setLength(double len)
{
len = length;
}
void Rectangle::setWidth(double wid)
{
wid = width;
}
double Rectangle::getLength()
{
return length;
}
double Rectangle::getWidth()
{
return width;
}
double Rectangle::calculateArea()
{
return (width*length)
}
double Rectangle::calculatePerimeter()
{
return ((width*2) + (length*2))
}
You already defined the functions in the class definition with a wrong return type (int instead of double)
class Rectangle
{
//...
int calculateArea() {return width*length;}
int calculatePerimeter() {return (width*2) + (length*2);}
//..
};
And then you redefined them outside the class
double Rectangle::calculateArea()
{
return (width*length)
}
double Rectangle::calculatePerimeter()
{
return ((width*2) + (length*2))
}
Also these functions are defined incorrectly
void Rectangle::setLength(double len)
{
len = length;
^^^^^^^^^^^^
}
void Rectangle::setWidth(double wid)
{
wid = width;
^^^^^^^^^^^
}
They must be defined like
void Rectangle::setLength(double len)
{
length = len;
}
void Rectangle::setWidth(double wid)
{
width = wid;
}
And all these functions
double getLength();
double getWidth();
int calculateArea() {return width*length;}
int calculatePerimeter() {return (width*2) + (length*2);}
should be declared with the qualifier const
double getLength() const;
double getWidth() const;
double calculateArea() const {return width*length;}
^^^^^^
double calculatePerimeter() const {return (width*2) + (length*2);}
^^^^^^
The compiler literally tells you!
It says that these two things don't match:
int Rectangle::calculateArea()
double Rectangle::calculateArea()
And, well, that's true. Your return types differ.
Pick one and use it consistently.

Debugging a Derived Class C++

When I run this code and create an instance of cylinderType by passing four parameters, debugger shows the height I want but, radius=x=y=0. So when I call method printVolume() on this object, it displays '0'.
Am I missing something important with inheritance?
Thank you~
#include <iostream>
using namespace std;
class circleType
{
public:
circleType();
circleType(double r);
double getArea() const;
private:
double radius;
};
class cylinderType : public circleType
{
public:
cylinderType(double h, double r);
void printVolume() const;
private:
double height;
};
int main()
{
cylinderType cylinderA(2, 4);
cylinderA.printVolume();
return 0;
};
circleType::circleType()
{
radius = 0;
};
circleType::circleType(double r)
{
radius = r;
};
double circleType::getArea() const
{
return (3.14 * radius* radius);
};
cylinderType::cylinderType(double h, double r)
{
circleType::circleType(r);
height = h;
};
void cylinderType::printVolume() const
{
cout << (circleType::getArea() * height);
};

Point, square, and cube program help on C++

I've been writing a program for CS class that's supposed to get the X and Y coordinates from the user, as well as the length of a square and the height of the cube, and it should then calculate the area of the square and the surface area and volume of the cube (plus some coordinates stuff but that's not a pressing issue right now)
I've written the test file and it compiled successfully, but I've been getting very long answers for the square and cube properties that are obviously wrong. Can anyone point out whatever logical errors I might have or if I have the access specification and relationship between the classes wrong?
Point.h
class Point
{
protected:
double Xint, Yint;
public:
Point();
void setX(double);
void setY(double);
double getX() const;
double getY() const;
};
Point.ccp
Point::Point()
{
Xint = 0;
Yint = 0;
}
void Point::setX(double x)
{ Xint = x; }
void Point::setY(double y)
{ Yint = y; }
double Point::getX() const
{ return Xint; }
double Point::getY() const
{ return Yint; }
Square.h
#include "Point.h"
class Square : public Point
{
protected:
Point lowerLeft;
double sideLength;
public:
Square(double sideLength, double x, double y) : Point()
{
sideLength = 0.0;
x = 0.0;
y = 0.0;
}
void setLowerLeft(double, double);
void setSideLength(double);
double getSideLength() const;
double getSquareArea() const;
};
Square.ccp
#include "Square.h"
void Square::setLowerLeft(double x, double y)
{
lowerLeft.setX(x);
lowerLeft.setY(y);
}
void Square::setSideLength(double SL)
{ sideLength = SL; }
double Square::getSideLength() const
{ return sideLength; }
// Calculate the area of square
double Square::getSquareArea() const
{ return sideLength * sideLength; }
Cube.h
#include "Square.h"
class Cube : public Square
{
protected:
double height;
double volume;
public:
Cube(double height, double volume) : Square(sideLength, Xint, Yint)
{
height = 0.0;
volume = 0.0;
}
double getSurfaceArea() const;
double getVolume() const;
};
Cube.ccp
#include "Cube.h"
// Redefine GetSquareArea to calculate the cube's surface area
double Cube::getSurfaceArea() const
{ return Square::getSquareArea() * 6; }
// Calculate the volume
double Cube::getVolume() const
{ return getSquareArea() * height; }
"Can anyone point out whatever logical errors I might have or if I have the access specification and relationship between the classes wrong?"
Well, from our well known 3-dimensional geometry a cube is made up from exactly 6 squares.
So how do you think inheriting a Cube class from a Square actually should work well?
You can easily define a Cube class by means of a fixed Point (e.g. the upper, left, front corner) and a fixed size of the edge length.
If you really want and need to, you can add a convenience function for your Cube class, that returns all of the 6 Squares it consist of in 3 dimensional space:
class Cube {
public:
Cube(const Point& upperLeftFrontCorner, double edgeLength);
std::array<Square,6> getSides() const;
};

How to use c++ class objects from .mm file in other file?

I have made two files that are MathUtils.h
#include "iostream"
and MathUtils.cpp
#include "MathUtils.h"
using namespace std;
//Box class .....................
class Box
{
private:
double length; // Length of a box
double breadth; // Breadth of a box
double height; // Height of a box
public:
void setParameters(int l,int b,int h);
int volume();
int area();
};
void Box::setParameters(int l, int b, int h)
{
length=l;
breadth=b;
height=h;
}
int Box::volume()
{
return length*breadth*height;
}
int Box::area()
{
return (2*(length*breadth) + 2*(breadth*height) + 2*(height*length));
}
//sphere class................
class Sphere
{
private:
double pi=3.14;
double r;
public:
void setParameters(int radius);
int volume();
int area();
};
void Sphere::setParameters(int radius)
{
r=radius;
}
int Sphere::volume()
{
return (4/3)*pi*r*r;
}
int Sphere::area()
{
return 4*pi*r*r;
}
How can we use this file in my project could any one help me.I have never use c++ files in my project so I want to know how can we use Box and Sphere class object in other viewController file.
Thanks!.
You define your classes in the .h file.
For your example, move:
class Box
{
private:
double length; // Length of a box
double breadth; // Breadth of a box
double height; // Height of a box
public:
void setParameters(int l,int b,int h);
int volume();
int area();
};
class Sphere
{
private:
double pi=3.14;
double r;
public:
void setParameters(int radius);
int volume();
int area();
};
to mathutils.h and add #include "mathutils.h" to your viewController file. Your member functions for Box should still be in mathutils.c
A function in your view controller can then make use of it:
{
Box b;
b.setParameters(1,2,3);
int v = b.volume();
int a = b.area();
}

C++ Composition program error(xcor undeclared )

Error!xcor undeclared at setAll() function
I had created object of PointType in Circle class to do sort of composition, but at the initialization of the constructor of Circle it is showing that
" [Error] 'ycor' was not declared in this scope "
" [Error] 'xcor' was not declared in this scope "
I want xcor and ycor in my Circle class in order to get the Radius using setAll() function
Please help!at what i am messing up.
#include<iostream>
#include<cstdlib>
#include<cmath>
using namespace std;
class PointType{
private:
int xcor;//x coordinate
int ycor;//y coordinate
public:
PointType();//constructor
PointType(int x,int y);
void setPoint(int x,int y);
int getx() const;
int gety() const;
};
PointType::PointType():xcor(0),ycor(0)
{
}
PointType::PointType(int x,int y):xcor(x),ycor(y){
}
void PointType::setPoint(int x,int y){
xcor=x;
ycor=y;
}
int PointType::getx() const{
return xcor;
}
int PointType::gety() const{
return ycor;
}
class Circle{
protected:
float Radius;
float Area;
int Circumference;
float pi;
PointType obj1;
public:
Circle();
void setAll();
float getRadius();
float getArea();
float getCircumference();
void callFunction();
void printAll();
void pt(int x,int y);
};
Circle::Circle():Radius(0),Area(0),Circumference(0),pi(3.1415),obj1(xcor,ycor){
}
void Circle::setAll(){
Radius=sqrt( (xcor*xcor) + (ycor*ycor) );
Area=pi*Radius*Radius;
Circumference=2*pi*Radius;
}
float Circle::getRadius(){
return Radius;
}
float Circle::getArea(){
return Area;
}
float Circle::getCircumference(){
return Circumference;
}
void Circle::printAll(){
cout<<"The Area is :"<<Area<<endl;
cout<<"The Circumference is :"<<Circumference<<endl;
}
void Circle::pt(int x,int y){
obj1.setPoint(x,y);
}
Your class Circle doesn't have any member variable called xcor or ycor. If you want to get the values of your PointType object, your setAll function should look like:
Radius=sqrt( (obj1.getx()*obj1.getx()) + (obj1.gety()*obj1.gety()) );
Also you have to change your constructor:
Circle::Circle():Radius(0),Area(0),Circumference(0),pi(3.1415),obj1(0,0)
as it doesn't have access to any xcor or ycor either.
You have to change your Circle constructor, because it knows nothing about xcor and ycor:
Circle::Circle():Radius(0),Area(0),Circumference(0),pi(3.1415),obj1(42,56)
The Circle class still doesn't know about xcor and ycor, so you have to change setAll method:
Radius=sqrt( static_cast<double>((obj1.getx()*obj1.getx()) + (obj1.gety()*obj1.gety())) );
declare xcor,ycor to Circle' constructor argument, and
add this line before computing Radius:
int xcor = obj1.getx(), ycor = obj1.gety();
Radius=sqrt( (xcor*xcor) + (ycor*ycor) );
You could also consider to inherit PointType for Circle, instead of embedded obj1:
class Circle : public PointType
{
public: Circle(int x, int y):PointType(x, y) {}
...
/* remove obj1 */
}