函数调用 C++ 中的参数太少
Too few arguments in function call C++
你好我是一名学习c++的学生,我才刚刚开始学习OOP。问题出在我的 MAIN 中,但是我显示了我的所有文件,以防它来自另一个文件。
我已经编写了我的 hpp 和 cpp 文件,现在我正在处理我的主要文件以进行测试。 class 称为 Box,当我创建一个对象 box1 或 box 2 并尝试访问我的函数时,它说有两个参数。无论我输入 box1.calcVolume(double h, double w, double l) 还是 box1.calcVolume();
,它都会这样说
所以问题出在这样的行上:
double volume2 = box2.calcVolume();
double volume1 = box1.calcVolume();
double surfaceArea1 = box1.calcSurfaceArea();
如果有人发现我遗漏或不理解的内容,请告诉我。
这是头文件:
#pragma once
#include <iostream>
#ifndef BOX_HPP
#define BOX_HPP
class Box
{
private:
double height;
double width;
double length;
public:
void setHeight(double h);
void setWidth(double w);
void setLength(double l);
double calcVolume(double h, double w, double l);
double calcSurfaceArea(double h, double w, double l);
Box();
Box(double height, double width, double length);
};
#endif
这是 CPP 文件
#include <iostream>
#include "Box.hpp"
Box::Box()
{
setHeight(1);
setWidth(1);
setLength(1);
}
Box::Box(double h, double w, double l)
{
setHeight(h);
setWidth(w);
setLength(l);
}
void Box::setHeight(double h)
{
height = h;
}
void Box::setWidth(double w)
{
width = w;
}
void Box::setLength(double l)
{
length = l;
}
double Box::calcVolume(double h, double w, double l)
{
double volume;
volume = h * w * l;
return volume;
}
double Box::calcSurfaceArea(double h, double w, double l)
{
double surfaceArea;
surfaceArea = 2 * (h*w) + 2 * (h*l) + 2 * (l*w);
return surfaceArea;
}
我的 BoxMain 文件:
#include <iostream>
#include "Box.hpp"
using std::cout;
using std::cin;
using std::endl;
int main()
{
Box box1(1.1, 2.4, 3.8);
Box box2;
box2.setHeight(12);
box2.setWidth(22.3);
box2.setLength(2.3);
double volume2 = box2.calcVolume();
double volume1 = box1.calcVolume();
double surfaceArea1 = box1.calcSurfaceArea();
cout << box1.calcVolume(); << endl; //testing different methods
return 0;
}
您的方法需要三个参数:
double Box::calcVolume(double h, double w, double l)
{
double volume;
volume = h * w * l;
return volume;
}
所以,你可以这样称呼它:
Box b;
double volume = b.calcVolume(1, 2, 3);
但这并不完全正确。 Box
的实例知道 它有多大,因为您将大小传递给构造函数,构造函数将大小存储在 width
、[=15= 字段中],以及 length
。你可能想要这样的东西:
double Box::calcVolume()
{
volume = height * width * length;
return volume;
}
您输入的密码有误。盒子的尺寸在盒子对象的创建过程中是已知的。长度、宽度和高度已经可用 - 在成员变量中更新。
函数 calcVolume
和 calcSurfaceArea
不应该接受参数,而是 return 计算值。修改后的代码:
double Box::calcVolume()
{
return height*width*length;
}
double Box::calcSurfaceArea()
{
return 2*((height*width) + (height*length) + (length*width));
}
此外,记得修改 .hpp 文件,使用与上述代码对应的声明。
.hpp 文件中的声明应该是
double calcVolume();
double calcSurfaceArea();
我已经解决了问题。我删除了代码中各处的 calcVolume 和 calcSurfaceArea 中的参数,它解决了错误。
你好我是一名学习c++的学生,我才刚刚开始学习OOP。问题出在我的 MAIN 中,但是我显示了我的所有文件,以防它来自另一个文件。
我已经编写了我的 hpp 和 cpp 文件,现在我正在处理我的主要文件以进行测试。 class 称为 Box,当我创建一个对象 box1 或 box 2 并尝试访问我的函数时,它说有两个参数。无论我输入 box1.calcVolume(double h, double w, double l) 还是 box1.calcVolume();
,它都会这样说所以问题出在这样的行上:
double volume2 = box2.calcVolume();
double volume1 = box1.calcVolume();
double surfaceArea1 = box1.calcSurfaceArea();
如果有人发现我遗漏或不理解的内容,请告诉我。
这是头文件:
#pragma once
#include <iostream>
#ifndef BOX_HPP
#define BOX_HPP
class Box
{
private:
double height;
double width;
double length;
public:
void setHeight(double h);
void setWidth(double w);
void setLength(double l);
double calcVolume(double h, double w, double l);
double calcSurfaceArea(double h, double w, double l);
Box();
Box(double height, double width, double length);
};
#endif
这是 CPP 文件
#include <iostream>
#include "Box.hpp"
Box::Box()
{
setHeight(1);
setWidth(1);
setLength(1);
}
Box::Box(double h, double w, double l)
{
setHeight(h);
setWidth(w);
setLength(l);
}
void Box::setHeight(double h)
{
height = h;
}
void Box::setWidth(double w)
{
width = w;
}
void Box::setLength(double l)
{
length = l;
}
double Box::calcVolume(double h, double w, double l)
{
double volume;
volume = h * w * l;
return volume;
}
double Box::calcSurfaceArea(double h, double w, double l)
{
double surfaceArea;
surfaceArea = 2 * (h*w) + 2 * (h*l) + 2 * (l*w);
return surfaceArea;
}
我的 BoxMain 文件:
#include <iostream>
#include "Box.hpp"
using std::cout;
using std::cin;
using std::endl;
int main()
{
Box box1(1.1, 2.4, 3.8);
Box box2;
box2.setHeight(12);
box2.setWidth(22.3);
box2.setLength(2.3);
double volume2 = box2.calcVolume();
double volume1 = box1.calcVolume();
double surfaceArea1 = box1.calcSurfaceArea();
cout << box1.calcVolume(); << endl; //testing different methods
return 0;
}
您的方法需要三个参数:
double Box::calcVolume(double h, double w, double l)
{
double volume;
volume = h * w * l;
return volume;
}
所以,你可以这样称呼它:
Box b;
double volume = b.calcVolume(1, 2, 3);
但这并不完全正确。 Box
的实例知道 它有多大,因为您将大小传递给构造函数,构造函数将大小存储在 width
、[=15= 字段中],以及 length
。你可能想要这样的东西:
double Box::calcVolume()
{
volume = height * width * length;
return volume;
}
您输入的密码有误。盒子的尺寸在盒子对象的创建过程中是已知的。长度、宽度和高度已经可用 - 在成员变量中更新。
函数 calcVolume
和 calcSurfaceArea
不应该接受参数,而是 return 计算值。修改后的代码:
double Box::calcVolume()
{
return height*width*length;
}
double Box::calcSurfaceArea()
{
return 2*((height*width) + (height*length) + (length*width));
}
此外,记得修改 .hpp 文件,使用与上述代码对应的声明。
.hpp 文件中的声明应该是
double calcVolume();
double calcSurfaceArea();
我已经解决了问题。我删除了代码中各处的 calcVolume 和 calcSurfaceArea 中的参数,它解决了错误。