Im practicing memberwise assignment in C++, where you can set the values of one object to another object of the same class. The idea of the program is to initialize a rectangle object with some values and create another rectangle object but assign the value of the first into the second.
Its giving me an error, which is posted below, and I can't figure out what it is and its driving me nuts lol
This is my Rectangle.h
#ifndef RECTANGLE_H
#define RECTANGLE_H
class Rectangle {
private:
double length;
double width;
public:
Rectangle(double, double);
double getLength() const;
double getWidth() const;
};
Rectangle::Rectangle(double l, double w) {
length = l;
width = w;
}
double Rectangle::getWidth() const { return width; }
double Rectangle::getLength() const { return length; }
#endif
This is my Rectangle.cpp
#include <iostream>
#include "rectangle.h"
using namespace std;
int main()
{
Rectangle box1(10.0, 10.0);
Rectangle box2;
cout << "box1's width and length: " << box1.getWidth() << ", " << box1.getLength() << endl;
cout << "box2's width and length: " << box2.getWidth() << ", " << box2.getLength() << endl;
box2 = box1;
cout << "box1's width and length: " << box1.getWidth() << ", " << box1.getLength() << endl;
cout << "box2's width and length: " << box2.getWidth() << ", " << box2.getLength() << endl;
return 0;
}
This is the error when I compile.
skipper~/Desktop/Programming/Memberwise: g++ rectangle.cpp
rectangle.cpp:7:12: error: no matching constructor for initialization of
'Rectangle'
Rectangle box1(10.0, 10.0);
^ ~~~~~~~~~~
./rectangle.h:4:7: note: candidate constructor (the implicit copy constructor)
not viable: requires 1 argument, but 2 were provided
class Rectangle {
^
./rectangle.h:4:7: note: candidate constructor
(the implicit default constructor) not viable: requires 0 arguments, but 2
were provided
1 error generated.
EDIT: This is how I was able to make it work. I moved everything into rectangle.cpp and gave the constructor default arguments.
EDITED rectangle.cpp
#include <iostream>
using namespace std;
class Rectangle {
private:
double length;
double width;
public:
//Rectangle();
Rectangle(double = 0.0, double = 0.0);
double getLength() const;
double getWidth() const;
};
int main()
{
Rectangle box1(10.0, 10.0);
Rectangle box2;
cout << "box1's width and length: " << box1.getWidth() << ", " << box1.getLength() << endl;
cout << "box2's width and length: " << box2.getWidth() << ", " << box2.getLength() << endl;
box2 = box1;
cout << "box1's width and length: " << box1.getWidth() << ", " << box1.getLength() << endl;
cout << "box2's width and length: " << box2.getWidth() << ", " << box2.getLength() << endl;
return 0;
}
Rectangle::Rectangle(double l, double w) {
length = l;
width = w;
}
double Rectangle::getWidth() const { return width; }
double Rectangle::getLength() const { return length; }
The only changes I made were giving default arguments to my user-defined constructor. However, it wasn't able to work when the changes were in rectangle.h. However, when I moved the class and member function definitions to rectangle.cpp it was able to work. So, I got the program to work but I didn't address the real issue, which is when the class and member function definitions are in rectangle.h, it won't compile.
If anyone has faced this problem and has found a solution to this, please let me know how you did it. Thanks :)
In the line
Rectangle box2; // no default constructor, error
you are trying to invoke the default constructor of Rectangle. The compiler does not generate such a default constructor anymore, because your Rectangle has a user defined constructor that takes 2 parameters. Therefore, you need to specify the parameters, like
Rectangle box2(0,10);
The error I get when compiling your code is:
Rectangle.cpp:8:15: error: no matching function for call to 'Rectangle::Rectangle()'
Rectangle box2;
A solution is to create a default constructor for Rectangle, since it is not automatically generated anymore due to your user defined one:
Rectangle(); // in Rectangle.h
Rectangle::Rectangle(){} // in Rectangle.cpp (or Rectangle::Rectangle() = default; in C++11)
Another solution (and the preferable one, since it doesn't leave the data un-initialized) is to assign default arguments to your existing constructor.
Rectangle::Rectangle(double l = 0, double w = 0); // only in Rectangle.h
In this way, you make your class Default-Constructible.
A compiler generated default constructor is only generated if you have no defined constructors. You define a constructor, so if you want a default constructor you have to provide it yourself. Probably the easiest (arguably) is to provide it by using default arguments in your two argument constructor:
Rectangle(double l=0, double w=0)
Also you should use the inline keyword as shown below or you may find you get linker errors:
inline Rectangle::Rectangle(double l, double w) {
length = l;
width = w;
}
inline double Rectangle::getWidth() const { return width; }
inline double Rectangle::getLength() const { return length; }
Related
Im practicing memberwise assignment in C++, where you can set the values of one object to another object of the same class. The idea of the program is to initialize a rectangle object with some values and create another rectangle object but assign the value of the first into the second.
Its giving me an error, which is posted below, and I can't figure out what it is and its driving me nuts lol
This is my Rectangle.h
#ifndef RECTANGLE_H
#define RECTANGLE_H
class Rectangle {
private:
double length;
double width;
public:
Rectangle(double, double);
double getLength() const;
double getWidth() const;
};
Rectangle::Rectangle(double l, double w) {
length = l;
width = w;
}
double Rectangle::getWidth() const { return width; }
double Rectangle::getLength() const { return length; }
#endif
This is my Rectangle.cpp
#include <iostream>
#include "rectangle.h"
using namespace std;
int main()
{
Rectangle box1(10.0, 10.0);
Rectangle box2;
cout << "box1's width and length: " << box1.getWidth() << ", " << box1.getLength() << endl;
cout << "box2's width and length: " << box2.getWidth() << ", " << box2.getLength() << endl;
box2 = box1;
cout << "box1's width and length: " << box1.getWidth() << ", " << box1.getLength() << endl;
cout << "box2's width and length: " << box2.getWidth() << ", " << box2.getLength() << endl;
return 0;
}
This is the error when I compile.
skipper~/Desktop/Programming/Memberwise: g++ rectangle.cpp
rectangle.cpp:7:12: error: no matching constructor for initialization of
'Rectangle'
Rectangle box1(10.0, 10.0);
^ ~~~~~~~~~~
./rectangle.h:4:7: note: candidate constructor (the implicit copy constructor)
not viable: requires 1 argument, but 2 were provided
class Rectangle {
^
./rectangle.h:4:7: note: candidate constructor
(the implicit default constructor) not viable: requires 0 arguments, but 2
were provided
1 error generated.
EDIT: This is how I was able to make it work. I moved everything into rectangle.cpp and gave the constructor default arguments.
EDITED rectangle.cpp
#include <iostream>
using namespace std;
class Rectangle {
private:
double length;
double width;
public:
//Rectangle();
Rectangle(double = 0.0, double = 0.0);
double getLength() const;
double getWidth() const;
};
int main()
{
Rectangle box1(10.0, 10.0);
Rectangle box2;
cout << "box1's width and length: " << box1.getWidth() << ", " << box1.getLength() << endl;
cout << "box2's width and length: " << box2.getWidth() << ", " << box2.getLength() << endl;
box2 = box1;
cout << "box1's width and length: " << box1.getWidth() << ", " << box1.getLength() << endl;
cout << "box2's width and length: " << box2.getWidth() << ", " << box2.getLength() << endl;
return 0;
}
Rectangle::Rectangle(double l, double w) {
length = l;
width = w;
}
double Rectangle::getWidth() const { return width; }
double Rectangle::getLength() const { return length; }
The only changes I made were giving default arguments to my user-defined constructor. However, it wasn't able to work when the changes were in rectangle.h. However, when I moved the class and member function definitions to rectangle.cpp it was able to work. So, I got the program to work but I didn't address the real issue, which is when the class and member function definitions are in rectangle.h, it won't compile.
If anyone has faced this problem and has found a solution to this, please let me know how you did it. Thanks :)
In the line
Rectangle box2; // no default constructor, error
you are trying to invoke the default constructor of Rectangle. The compiler does not generate such a default constructor anymore, because your Rectangle has a user defined constructor that takes 2 parameters. Therefore, you need to specify the parameters, like
Rectangle box2(0,10);
The error I get when compiling your code is:
Rectangle.cpp:8:15: error: no matching function for call to 'Rectangle::Rectangle()'
Rectangle box2;
A solution is to create a default constructor for Rectangle, since it is not automatically generated anymore due to your user defined one:
Rectangle(); // in Rectangle.h
Rectangle::Rectangle(){} // in Rectangle.cpp (or Rectangle::Rectangle() = default; in C++11)
Another solution (and the preferable one, since it doesn't leave the data un-initialized) is to assign default arguments to your existing constructor.
Rectangle::Rectangle(double l = 0, double w = 0); // only in Rectangle.h
In this way, you make your class Default-Constructible.
A compiler generated default constructor is only generated if you have no defined constructors. You define a constructor, so if you want a default constructor you have to provide it yourself. Probably the easiest (arguably) is to provide it by using default arguments in your two argument constructor:
Rectangle(double l=0, double w=0)
Also you should use the inline keyword as shown below or you may find you get linker errors:
inline Rectangle::Rectangle(double l, double w) {
length = l;
width = w;
}
inline double Rectangle::getWidth() const { return width; }
inline double Rectangle::getLength() const { return length; }
I was doing c++ tutorials in cplusplus.com but some of the codes are not working. And here is one of the code that I'm struggling most.
It's about initialization of array of classes.
I've tried a lot of different ways that I found in web but this code still couldn't be compiled. Of course I can just use for loop, but I really wonder why I can't use this kind of initialization in my c++. Below codes are exactly same code from http://www.cplusplus.com/doc/tutorial/classes/
// pointer to classes example
#include <iostream>
using namespace std;
class Rectangle {
int width, height;
public:
Rectangle(int x, int y) : width(x), height(y) {}
int area(void) { return width * height; }
};
int main() {
Rectangle obj (3, 4);
Rectangle * foo, * bar, * baz;
foo = &obj;
bar = new Rectangle (5, 6);
baz = new Rectangle[2] { {2,5}, {3,6} };
cout << "obj's area: " << obj.area() << '\n';
cout << "*foo's area: " << foo->area() << '\n';
cout << "*bar's area: " << bar->area() << '\n';
cout << "baz[0]'s area:" << baz[0].area() << '\n';
cout << "baz[1]'s area:" << baz[1].area() << '\n';
delete bar;
delete[] baz;
return 0;
}
compiler error says,
class_example.cpp:30:13: error: no matching constructor for
initialization of 'Rectangle [2]'
baz = new Rectangle[2] { {2,5}, {3,6} };
^
class_example.cpp:17:7: note: candidate constructor (the
implicit copy constructor) not viable: requires 1
argument, but 0 were provided
class Rectangle {
^
class_example.cpp:20:3: note: candidate constructor not
viable: requires 2 arguments, but 0 were provided
Rectangle(int x, int y) : width(x), height(y) {}
^
1 error generated.
enter code here
Is this because of my c++ version? I think my version is, when I type
c++ --version in terminal It shows me,
Apple LLVM version 10.0.0 (clang-1000.10.44.4)
Target: x86_64-apple-darwin17.4.0
Thread model: posix
Closed. This question is not reproducible or was caused by typos. It is not currently accepting answers.
This question was caused by a typo or a problem that can no longer be reproduced. While similar questions may be on-topic here, this one was resolved in a way less likely to help future readers.
Closed 4 years ago.
Improve this question
Here is the code that I have so far:
#include <iostream>
using namespace std;
class Rectangle
{
private:
double width, height;
public:
Rectangle();
double Set(double x, double y);
double getArea();
double getPerimeter();
};
Rectangle::Rectangle()
{
width = 1;
height = 1;
}
double Rectangle::Set(double x, double y)
{
width = x;
height = y;
}
double Rectangle::getArea()
{
double area = height * width;
return area;
}
double Rectangle::getPerimeter()
{
double perimeter = (width * 2) + (height * 2);
return perimeter;
}
int main()
{
double width, height;
cout << "Enter the width and height of a rectangle:";
cin >> width >> height;
cout << "The area is " << Rectangle::getArea << " and the perimeter is " << Rectangle::getPerimeter << endl;
}
When running this code I get the error : "'Rectangle::getArea': non-standard syntax; use '&' to create a pointer to member"
I get the same error about the Rectangle::getPerimeter
I am not sure what the problem is, I am new to making classes in C++ obviously, so I am having some trouble. Any suggestions?
There are several problems with your code.
1.) You never instantiate the class (i.e. create an object of that class type.) Having two variables with the names width and height is not the same as having an object of the type Rectangle.
2.) You are trying to call a non-static member function as if they were static member functions. Member functions are called via objects, for example
rect.getArea() where rect is an object of the type Rectangle.
3.) You are missing the parentheses at the function call. Whenever you get the message non-standard syntax, use & to create a pointer to a member it usually means that you have forgotten the parentheses at a function call.
What you want is probably something like:
int main()
{
double width, height;
cout << "Enter the width and height of a rectangle:";
cin >> width >> height;
Rectangle rect;
rect.Set(width, height);
cout << "The area is " << rect.getArea() << " and the perimeter is " << rect.getPerimeter() << endl;
}
1st create the object of Rectangle() class and then call the respective methods.
Rectangle rect;/* object of rectangle class */
your main() looks like
int main() {
double width, height;
cout << "Enter the width and height of a rectangle:";
cin >> width >> height;
Rectangle rect;/* object of rectangle class */
/* calling set method and passing the parameter */
rect.Set(width,height);
cout << "The area is " << rect.getArea() << " and the perimeter is " << rect.getPerimeter() << endl;
return 0;
}
As If i understood correctly you don't need Set() method actually, instead of this you do the same stuff using constructor by passing parameters.
parameterized constructor instead of Set() method
Rectangle::Rectangle(double x, double y) {
width = x;
height = y;
}
And create objects like
Rectangle rect(width,height);/* it will be called automatically */
For starters this member function returns nothing
double Rectangle::Set(double x, double y)
{
width = x;
height = y;
}
So it should be defined like
void Rectangle::Set(double x, double y)
{
width = x;
height = y;
}
It seems you mean
//...
cin >> width >> height;
Rectangle r;
r.Set( width, height );
cout << "The area is " << r.getArea() << " and the perimeter is " << r.getPerimeter() << endl;
The functions getArea and getPerimeter should be declared as constant member functions
double getArea() const;
double getPerimeter() const;
because they do not change objects of the type Rectangle.
It would be logical consistent if the class had a constructor with two parameters
Rectangle( double width, double height );
And the method Set should be split into two methods like setWidth and setHeight.
I'm currently working on my C++ assignment and I'm working through polymorphism and I keep getting error messages. Would appreciate any help. Thanks in advance!
The assignment uses a shape inheritance hierarchy that looks like this:
Circle
Two Dimensional Shape
Cylnder
Shape
Three Dimensional Shape
Below is the error messages I'm getting.
1>Circle.obj : error LNK2019: unresolved external symbol "public: __thiscall TwoDimensionalShapes::TwoDimensionalShapes(void)" (??0TwoDimensionalShapes##QAE#XZ) referenced in function "public: __thiscall Circle::Circle(void)" (??0Circle##QAE#XZ)
1>Cylinder.obj : error LNK2019: unresolved external symbol "public: __thiscall ThreeDimensionalShapes::ThreeDimensionalShapes(void)" (??0ThreeDimensionalShapes##QAE#XZ) referenced in function "public: __thiscall Cylinder::Cylinder(void)" (??0Cylinder##QAE#XZ)
I'm pretty much done with my program but I'm not sure where the problems are coming from.
Here's my source code:
#include "Cylinder.h"
#include "Circle.h"
#include <iostream>
#include <iomanip>
using namespace std;
int main()
{
// next line commented out because will now cause compile error
// Shape shpeObj; // instantiate a Shape object
cout << "The Shape object count is: " << Shape::getObjectCount() << '\n';
Circle crclObj; // instantiate a Circle object
Cylinder cyldObj; // instantiate a Cylinder object
// Count will be 2 in statement below because a Shape object is contained
// within each Circle and Cylinder object.
cout << "The Shape object count is: " << Shape::getObjectCount() << "\n";
Shape * shpePtr = &cyldObj; // declare a Shape pointer
// and have it point to the Cylinder object
Shape & shpeRef = cyldObj; // declare a Shape reference
// and have it reference the Cylinder object
// The above 2 statments are ok because a derived class object IsA base class object!
// No additional objects created, so the count is still the same.
cout << "The Shape object count is: " << Shape::getObjectCount() << "\n";
// Dynamically create 2 objects
Shape * shpePtr2 = new Circle;
Shape & shpeRef2 = *new Cylinder;
// The count should now be 4
cout << "The Shape object count is: " << Shape::getObjectCount() << "\n";
// Now destroy the 2 dynamically created objects!
delete shpePtr2;
delete &shpeRef2;
//The count should now be 2 again.
cout << "The Shape object count is: " << Shape::getObjectCount() << "\n\n";
/* Can no longer test Shape class, since it is now an Abstract Base Class (ABC)
// Test Shape class
shpeObj.setNoOfSides(0);
cout << "The number of sides is: " << shpeObj.getNoOfSides() << "\n\n";
cout << "The area of shpeObj is: " << shpeObj.Area() << '\n';
cout << "The volume of shpeObj is: " << shpeObj.Volume() << "\n\n\n";
*/
// Test Circle class
crclObj.setRadius(3.0);
cout << fixed << setprecision(4); // force use of decimal point and 4 digits of
// precision after the decimal place
cout << "The radius of crclObj is: " << crclObj.getRadius() << "\n\n";
cout << "The number of sides is: " << crclObj.getNoOfSides() << "\n\n";
cout << "The area of crclObj is: " << crclObj.Area() << '\n';
// next line no longer valid in my solution for this assignment
//cout << "The volume of crclObj is: " << crclObj.Volume() << "\n\n\n";
// Test Cylinder class
cyldObj.setRadius(5.5);
cyldObj.setHeight(2.5);
cout << "The radius of cyldObj is: " << cyldObj.getRadius() << '\n';
cout << "The height of cyldObj is: " << cyldObj.getHeight() << "\n\n";
cout << "The number of sides is: " << cyldObj.getNoOfSides() << "\n\n";
cout << "The area of cyldObj is: " << cyldObj.Area() << '\n';
cout << "The volume of cyldObj is: " << cyldObj.Volume() << "\n\n";
return 0;
}
#pragma once
class Shape
{
public:
Shape(void); //constructor
~Shape(void); //destructor
void setNoOfSides(const int &); // set the # of sides
int getNoOfSides() const; // get the # of sides
static int getObjectCount(); // get the object count
virtual double Area() const = 0; // calculate and return area
// now a pure virtual function
virtual double Volume() const; // calculate and return volume
protected:
int mNoOfSides; // represents # of sides in Shape object
static int mObjectCount; // a static member - counts the # of Shape
// objects currently instantiated
// Only one of these instantiated for the whole class!
};
#pragma once
#include "Shape.h"
class TwoDimensionalShapes :public Shape
{
public:
TwoDimensionalShapes(void); //constructor
virtual double Area() const = 0; // area of 2D shape
};
#pragma once
#include "Shape.h"
class ThreeDimensionalShapes :public Shape
{
public:
ThreeDimensionalShapes(void); // constructor
virtual double Area() const = 0; // area of 3D shape
virtual double Volume() const = 0; // volume of 3D shape
};
#pragma once
#include "TwoDimensionalShapes.h"
class Circle :public TwoDimensionalShapes
{
public:
Circle(void); // constructor
void setRadius(const double &); //set the radius
double getRadius() const; // get the radius
virtual double Area() const override; // overrides Area() method of TwoDimensionalShapes class
protected:
const static double pi; // Static member used in calculations
// Only one of these instantiated for the whole class!
double mRadius; // member used to represent radius
};
#pragma once
#include "ThreeDimensionalShapes.h"
class Cylinder :public ThreeDimensionalShapes
{
public:
Cylinder(void); // constructor
void setHeight(const double &); // set the height
double getHeight() const; // get the height
void setRadius(const double &); //set the radius
double getRadius() const; // get the radius
virtual double Area() const override; // overrides Area() method of ThreeDimensionalShapes class
virtual double Volume() const override; // overrides Volume() method of ThreeDimensionalShapes class
protected:
const static double pi; // Static member used in calculations
// Only one of these instantiated for the whole class!
double mHeight; // member used to represent height
double mRadius;
};
#include "Circle.h"
// init static data member
const double Circle::pi = 3.141592654; // init. static member
// constructor
Circle::Circle(void)
:mRadius(0.0)
{
setNoOfSides(0);
}
// used to set value for mRadius member
void Circle::setRadius(const double & setVal)
{
if (setVal > 0.0) // Make sure input is a valid value
{
this->mRadius = setVal;
}
// otherwise just leave set to original value
}
// used to return current value of mRadius member
double Circle::getRadius(void) const
{
return this->mRadius;
}
// used top calculate and return area.
double Circle::Area(void) const
{
return Circle::pi * this->mRadius * this->mRadius;
}
#include "Cylinder.h"
// init static data member
const double Cylinder::pi = 3.141592654; // init. static member
// constructor
Cylinder::Cylinder(void)
:mHeight(0.0)
{
this->setNoOfSides(3); // Why not init. this member in MIL ???
}
// used to set mHeight member
void Cylinder::setHeight(const double & setVal)
{
if (setVal > 0.0) // Make sure input is a valid value
{
this->mHeight = setVal;
}
// otherwise just leave set to original value
}
// used to return current value of mHeight member
double Cylinder::getHeight(void) const
{
return this->mHeight;
}
// used to set value for mRadius member
void Cylinder::setRadius(const double & setVal)
{
if (setVal > 0.0) // Make sure input is a valid value
{
this->mRadius = setVal;
}
// otherwise just leave set to original value
}
// used to return current value of mRadius member
double Cylinder::getRadius(void) const
{
return this->mRadius;
}
// used to caluclate and return area
double Cylinder::Area(void) const
{
double TwoPiR = 2.0 * Cylinder::pi * this->mRadius;
return (TwoPiR * this->mRadius) + (TwoPiR * this->mHeight);
}
// used to claculate and return volume
double Cylinder::Volume(void) const
{
return Cylinder::pi * this->mRadius * this->mRadius * this->mHeight;
}
#include "Shape.h"
// init static data memeber
int Shape::mObjectCount = 0;
// constructor
Shape::Shape(void)
:mNoOfSides(1)
{
++Shape::mObjectCount;
}
// desstructor
Shape::~Shape(void)
{
--Shape::mObjectCount;
}
// used to set mNoOfSides member
void Shape::setNoOfSides(const int & setVal)
{
if (setVal > 0)
{
this->mNoOfSides = setVal;
}
// otherwise just leave set to original value
}
// used to return current value of mNoOfSides member
int Shape::getNoOfSides() const
{
return this->mNoOfSides;
}
// used to return current value of mObjectCount static member
int Shape::getObjectCount()
{
return Shape::mObjectCount;
}
/* no longer required to be implemented now that it is a
pure virtual function
// used to calculate and return area
double Shape::Area(void) const
{
return 0.0;
}
*/
// used to calculate and return volume
double Shape::Volume(void) const
{
return 0.0;
}
OK I figured it out now. I forgot to provide a constructor implementation for my TwoDimensionalShapes and ThreeDimensionalShapes classes. Silly mistake.
I am getting this annoying error and I don't know why =( !
This is the question , I solved it but I am having a problem with the constructor.
Write a program that defines a class called Circle that includes radius (type double) as data members. Provide a set and a get function
for this data member. Ensure that the value entered by the user is
valid and correct (greater than zero).
Include function members: a.function member that compute and return Diameter of the circle b.function member that compute and
return Circumference of the circle c.function member that compute and
return Area of the circle d.function member that Display all
information of the circle e.constructor that initializes the data
member. If the radius is not valid (i.e. less than zero) set it to
zero.
the error I am facing :
error C2512: 'Circle' : no appropriate default constructor available
this is my code :
#include <iostream>
using namespace std;
class Circle
{
public:
Circle(double);
void setRadius(double);
double getRadius();
void Display();
double Diameter(double);
double Circumference(double);
double Area(double);
private:
double radius;
};
Circle::Circle(double radio)
{
setRadius(radio);
}
void Circle::setRadius(double ra)
{
if (ra < 0)
{
radius = 0;
}
else
radius = ra;
}
double Circle::getRadius()
{
double rado;
cout << "Enter the Radius:\n";
cin >> rado;
setRadius(rado);
return radius;
}
double Circle::Diameter(double rad)
{
return 2*rad;
}
double Circle::Area(double radi)
{
return 3.14 * radi * radi;
}
double Circle::Circumference(double radiu)
{
return 2 * 3.14 * radiu;
}
void Circle::Display()
{
cout << "The Radius of the circle is: \n";
cout << radius;
cout << "\nThe Diameter of the circle is: \n";
cout << Diameter(radius);
cout << "\nThe Circumference of the circle is: \n";
cout << Circumference(radius);
cout << "\nThe Area of the circle is: \n";
cout << Area(radius);
cout << endl;
}
int main()
{
Circle C;
C.getRadius();
C.Display();
return 0;
}
This line invokes a constructor with no arguments (known as default constructor):
Circle C;
The only constructor you have defined is:
Circle(double);
Hopefully this should point you in the right direction.
A default constructor is one without any parameters. Normally, it is provided for you. But if you explicitly define any other constructor, then it is not. So you have to define it yourself, or not use it. You are using it when you create an object in main, like this:
Circle C;
So, either define a default constructor, or don't use it.
Well, then add one :)
Circle() : radius(0.0) {}
You should define a constructor with no parameters called default constructor. You can initialize related members to the default values.
Circle::Circle()
{
radius = 0.0
}