错误C2512:没有合适的默认构造函数可用

时间:2020-12-27 18:57:26

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.

编写一个程序,定义一个名为Circle的类,该类包含radius(类型double)作为数据成员。为这个数据成员提供一个集合和一个get函数。确保用户输入的值是有效且正确的(大于0)。包括函数成员:一个。函数成员,计算并返回圆b的直径。函数成员,计算并返回圆c的周长。函数成员,计算和返回圆d的面积。函数成员,显示圆圈e的所有信息。初始化数据成员的构造函数。如果半径无效(即小于零),则将其设为零。

the error I am facing :

我所面对的错误:

error C2512: 'Circle' : no appropriate default constructor available

错误C2512: 'Circle':没有合适的默认构造函数可用

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;
        }

4 个解决方案

#1


14  

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.

希望这能让你找到正确的方向。

#2


5  

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.

因此,要么定义默认构造函数,要么不使用它。

#3


2  

Well, then add one :)

然后加上一个:)

Circle() : radius(0.0) {}

#4


-1  

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
   }

#1


14  

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.

希望这能让你找到正确的方向。

#2


5  

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.

因此,要么定义默认构造函数,要么不使用它。

#3


2  

Well, then add one :)

然后加上一个:)

Circle() : radius(0.0) {}

#4


-1  

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
   }