17

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;
        }
Angel Angel
  • 19,670
  • 29
  • 79
  • 105
user1092492
  • 181
  • 1
  • 2
  • 5
  • 2
    I think you misunderstood how the member functions should look like. Your code doesn’t follow a particularly object-oriented style. For instance, I cannot get the area of circle `C` by calling `C.Area()`. You should fix that. Also, is there a particular reason that all your arguments have different names even thought they *all* refer to the radius? Don’t do that, be consistent. – Konrad Rudolph Dec 31 '11 at 17:11
  • Your constructor tries to use a member function, for that, there must already be an object, so a constructor needs to have finished before. You should initialize member variables using a constructor of their type before entering the constructor body. – Daniel Fischer Dec 31 '11 at 17:21

4 Answers4

19

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.

hmjd
  • 120,187
  • 20
  • 207
  • 252
6

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.

Benjamin Lindley
  • 101,917
  • 9
  • 204
  • 274
4

Well, then add one :)

Circle() : radius(0.0) {}
marcinj
  • 48,511
  • 9
  • 79
  • 100
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
   }
Rabia
  • 19
  • 3
  • Class member variables should be initialised via initialisation list (like luskan has done), and not in the constructor body - unless there's a very good reason not to (I see no such reason here). If radius was a non-POD, it could be initialised TWICE. – brewmanz Jan 04 '16 at 21:53