gpt4 book ai didi

c++ - 如何解决此错误的调用不匹配功能?

转载 作者:行者123 更新时间:2023-12-02 10:19:26 25 4
gpt4 key购买 nike

#include <iostream>
#include <cmath>
#include <cstdlib>
using namespace std;

class Shape3D
{
public:
virtual double volumn() ;
virtual double surfaceArea() ;
};

class RightRectangularPyramid : public Shape3D
{
private:
double width;
double length;
double height;

public:
RightRectangularPyramid(double width, double length, double height) : Shape3D(width, length, height) {
this -> width = 0.0;
this -> length = 0.0;
this -> height = 0.0;
}

double volumn() override{
return width * length * height ;
}

double surfaceArea() override{
return (length * width) + (length * sqrt(height*height + (width/2.0)*(width/2.0))) + (width * sqrt(height*height + (length/2.0)*(length/2.0)));
}
};

class Sphere : public Shape3D
{
private:
double radius;

public:
Sphere(double radius){
this -> radius = radius;
}

double volumn() override{
return 4.0/3.0 * M_PI * pow(radius,2);
}

double surfaceArea() override{
return 4 * M_PI * pow(radius,2);
}

};

int main()
{
int tc;
cin >> tc;
if (tc == 1) {
Shape3D **shapes = new Shape3D*[3];
shapes[0] = new RightRectangularPyramid(1, 5.2, 7);
shapes[1] = new Sphere(10.6);
shapes[2] = new RightRectangularPyramid(3, 3.5, 4.13);
for (int i = 0; i < 3; i++) {
cout << "Volumn: " << (*shapes)->volumn() << endl;
cout << "Surface Area: " << (*shapes)->surfaceArea() << endl;
cout << "--------------------\n";
*shapes++;
}
} else if (tc == 2) {
RightRectangularPyramid rectangle;
Sphere sphere;
cout << rectangle.volumn() << endl;
cout << rectangle.surfaceArea() << endl;
cout << sphere.volumn() << endl;
cout << sphere.surfaceArea() << endl;
} else if (tc == 3) {
RightRectangularPyramid rectangle(171.5, 45.33, 31.02);
cout << rectangle.volumn() << endl;
cout << rectangle.surfaceArea() << endl;
} else if (tc == 4) {
Sphere sphere(34.25);
cout << sphere.volumn() << endl;
cout << sphere.surfaceArea() << endl;
} else if (tc == 5) {
RightRectangularPyramid rectangle(171.5, 45.33, 31.02);
Sphere sphere(34.25);
Shape3D *shape = &rectangle;
cout << shape->volumn() << " " << shape->surfaceArea() << endl;
shape = &sphere;
cout << shape->volumn() << " " << shape->surfaceArea() << endl;
} else if(tc == 6) {
}
}

任何错误

错误:没有匹配的函数调用'Shape3D::Shape3D(double&,double&,double&)'
RightRectangularPyramid(双倍宽度,双倍长度,双倍高度):Shape3D(宽度,长度,高度)

错误:没有匹配的函数来调用“RightRectangularPyramid::RightRectangularPyramid()”
RightRectangularPyramid矩形;

错误:没有匹配的函数可以调用'Sphere::Sphere()'
球体

还有这个更多的错误
https://ibb.co/nrV3H3p

最佳答案

这里

RightRectangularPyramid(double width, double length, double height) : Shape3D(width, length, height) {

由于某种原因,您试图将宽度的长度和高度传递给不存在的Shape3D构造函数。只需删除构造函数调用
RightRectangularPyramid(double width, double length, double height) {

这里
RightRectangularPyramid rectangle;

您试图创建一个没有宽度,长度和高度的金字塔,但是您编写了需要宽度,长度和高度的 RightRectangularPyramid构造函数。您需要添加这些值,例如
RightRectangularPyramid rectangle(1.0, 2.0, 3.0);

在这里,同样的问题
Sphere sphere;

您试图创建没有半径的球体,但是您编写了需要半径的 Sphere构造函数。因此,您需要提供一个,例如
Sphere sphere(99.9);

有时,编译器错误消息是含糊不清的,但这些错误消息似乎很明显,编译器会告诉您确切的错误之处。

关于c++ - 如何解决此错误的调用不匹配功能?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/60882454/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com