C++不存在从 到 的适当转换函数

 

#include<iostream>
#include<iomanip>
using namespace std;
class Point
{
public:
    double X, Y;
    Point(double x, double y);
    double GetX();
    double GetY();
    double SetPoint(double x0, double y0);
    void Show();

};
Point::Point(double x=0, double y=0) :X(x), Y(y)
{

}
double Point::GetX()
{
    return X;
}
double Point::GetY()
{
    return Y;
}
double Point::SetPoint(double x0, double y0) 
{
    X = x0;
    Y = y0;
}
void Point::Show()
{
    cout << X << " " << Y << endl;
}
class Circle :public Point
{
public:
    double m_X,m_Y,R;

    double Girth();
    double Area();
    void Show();
    Circle(double m_x, double m_y, double r);
};
Circle:: Circle(double m_x, double m_y, double r)  
{
    m_X = m_x;
    m_Y = m_y;
    R = r;

}
double Circle::Girth()
{
    return 2 * 3.14 * R;
}
double Circle::Area()
{
    return 3.14 * R * R;
}
void Circle::Show()
{
    cout << "(" << m_X << "," << m_Y << ")" << endl;
    cout << R;
}
class Cylinder :public Circle
{
public:
    Cylinder();
    double H, Feature;
    double SetCylinder(Circle(double m_x, double m_y, double r), double h);
    double Area();
    double Volumn();
    void Show();
};
double Cylinder::SetCylinder(Circle(double m_x, double m_y, double r),double h)
{
    H = h;
    
}
double Cylinder::Area()
{
    return 2 * 2 * Circle::R * Circle::R + 2 * 3.14 * Circle::R * H;
}
double Cylinder::Volumn()
{
    return 3.14 * Circle::R * Circle::R * H;
}
void Cylinder::Show()
{
     cout << "(" << m_X << "," << m_Y << ")" << endl;
     cout << R << endl;
     cout << H << endl;
}
int  main()
{
    double  x, y;
    cin >> x >> y;//若输入:10  20
    Point  p1(x, y);
    p1.SetPoint(x + 10, y + 5);
    cout << fixed << setprecision(1);
    p1.Show();//单独占一行输出:10,20


    Circle c1(p1.GetX(), p1.GetY(), 20);
    c1.Show();//输出两行:圆心一行,半径一行
    cout << "c1的周长是:" << c1.Girth() << endl;//一个数据一行
    cout << "c1的面积是:" << c1.Area() << endl;//一个数据一行


    Cylinder  cy1;
    cy1.SetCylinder(c1, 10.5);
    cy1.Show();//输出三行:圆心一行,半径一行,高一行
    cout << "cy1的表面积是:" << cy1.Area() << endl;//一个数据一行
    cout << "cy1的体积是:" << cy1.Volumn() << endl;//一个数据一行
    return  0;
}

建议将报错内容贴出来。

你的Set函数参数定义有问题,写成函数指针类型了,应该是

SetCylinder(Circle c1,double h)

关键是第70行的问题 double SetCylinder(Circle(double m_x, double m_y, double r), double h);

应该定义喂:double SetHeight(double h);

 

而在Cylinder的构造函数提供父类Circle的double m_x, double m_y, double r

我修改了构造函数,运行通过了。

#include<iostream>
#include<iomanip>
using namespace std;
class Point
{
public:
    double X, Y;
    Point(double x, double y);
    double GetX();
    double GetY();
    void SetPoint(double x0, double y0);
    void Show();

};
Point::Point(double x=0, double y=0) :X(x), Y(y)
{
}
double Point::GetX()
{
    return X;
}
double Point::GetY()
{
    return Y;
}
void Point::SetPoint(double x0, double y0) 
{
    X = x0;
    Y = y0;
}
void Point::Show()
{
    cout << X << " " << Y << endl;
}
class Circle :public Point
{
public:
    double m_X,m_Y,R;

    double Girth();
    double Area();
    void Show();
    Circle(double m_x, double m_y, double r);
};
Circle:: Circle(double m_x, double m_y, double r)  
{
    m_X = m_x;
    m_Y = m_y;
    R = r;

}
double Circle::Girth()
{
    return 2 * 3.14 * R;
}
double Circle::Area()
{
    return 3.14 * R * R;
}
void Circle::Show()
{
    cout << "(" << m_X << "," << m_Y << ")" << endl;
    cout << R;
}
class Cylinder :public Circle
{
public:
    Cylinder(double m_x, double m_y, double r, double h);
    Cylinder(const Circle& circle, double h);
    double H, Feature;
    void SetHeight(double h);
    double Area();
    double Volumn();
    void Show();
};
Cylinder::Cylinder(double m_x, double m_y, double r, double h): Circle(m_x, m_y, r)
{
    H = h;
}
Cylinder::Cylinder(const Circle& circle, double h): Circle(circle)
{
    H = h;
}
void Cylinder::SetHeight(double h)
{
    H = h;
    
}
double Cylinder::Area()
{
    return 2 * 2 * Circle::R * Circle::R + 2 * 3.14 * Circle::R * H;
}
double Cylinder::Volumn()
{
    return 3.14 * Circle::R * Circle::R * H;
}
void Cylinder::Show()
{
     cout << "(" << m_X << "," << m_Y << ")" << endl;
     cout << R << endl;
     cout << H << endl;
}
int  main()
{
    double  x, y;
    cin >> x >> y;//若输入:10  20
    Point  p1(x, y);
    p1.SetPoint(x + 10, y + 5);
    cout << fixed << setprecision(1);
    p1.Show();//单独占一行输出:10,20


    Circle c1(p1.GetX(), p1.GetY(), 20);
    c1.Show();//输出两行:圆心一行,半径一行
    cout << "c1的周长是:" << c1.Girth() << endl;//一个数据一行
    cout << "c1的面积是:" << c1.Area() << endl;//一个数据一行


    Cylinder  cy1(c1, 10.5);
    cy1.Show();//输出三行:圆心一行,半径一行,高一行
    cout << "cy1的表面积是:" << cy1.Area() << endl;//一个数据一行
    cout << "cy1的体积是:" << cy1.Volumn() << endl;//一个数据一行
    return  0;
}

// Output:
10 20                                                                                                                                                    
20.0 25.0                                                                                                                                                
(20.0,25.0)                                                                                                                                              
20.0c1的周长是:125.6                                                                                                                                    
c1的面积是:1256.0                                                                                                                                       
(20.0,25.0)                                                                                                                                              
20.0                                                                                                                                                     
10.5                                                                                                                                                     
cy1的表面积是:2918.8                                                                                                                                    
cy1的体积是:13188.0   

 

1.你定义的函数,返回值l类型是double的,必须return 一下。或者把double Point::SetPoint(double x0, double y0) 函数修改为void Point::SetPoint(double x0, double y0) 。其它函数类同。

2.将double SetCylinder(Circle(double m_x, double m_y, double r), double h);按照如下方法修改:(如有帮助,请采纳,谢谢)


class Cylinder :public Circle
{
public:
	Cylinder();
	double H, Feature;
	double SetCylinder(Circle cl, double h); //double SetCylinder(Circle(double m_x, double m_y, double r), double h);
	double Area();
	double Volumn();
	void Show();
};

double Cylinder::SetCylinder(Circle cl,double h) //(double m_x, double m_y, double r)
{
	H = h;
	return 0;
}

您好,我是有问必答小助手,您的问题已经有小伙伴解答了,您看下是否解决,可以追评进行沟通哦~

如果有您比较满意的答案 / 帮您提供解决思路的答案,可以点击【采纳】按钮,给回答的小伙伴一些鼓励哦~~

ps:问答VIP仅需29元,即可享受5次/月 有问必答服务,了解详情>>>https://vip.csdn.net/askvip?utm_source=1146287632