C++ 错误 C2512:没有合适的默认构造函数可用
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/8689319/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
error C2512: no appropriate default constructor available
提问by user1092492
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 的类,该类包含半径(double 类型)作为数据成员。为这个数据成员提供一个 set 和一个 get 函数。确保用户输入的值有效且正确(大于零)。
包括函数成员: a. 计算和返回的函数成员 圆的直径 b. 计算和返回的函数成员 圆的周长 c. 计算和返回的函数成员 圆的面积 d. 显示所有信息的函数成员初始化数据成员的 circle e.constructor。如果半径无效(即小于零)将其设置为零。
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;
}
回答by hmjd
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.
希望这应该为您指明正确的方向。
回答by Benjamin Lindley
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:
默认构造函数是一个没有任何参数的构造函数。通常,它是为您提供的。但是,如果您显式定义了任何其他构造函数,则它不是。所以你必须自己定义它,或者不使用它。在 main 中创建对象时正在使用它,如下所示:
Circle C;
So, either define a default constructor, or don't use it.
因此,要么定义默认构造函数,要么不使用它。
回答by marcinj
Well, then add one :)
好吧,然后加一个:)
Circle() : radius(0.0) {}
回答by Rabia
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
}