C++ 如何修复包含必须返回值的错误?
只是一个简单的问题,我不知道为什么会出现这个错误或者如何修复它 错误是C4176 Box::getParameters:必须返回一个值。它说它必须返回值,但我确实把getParameter放在那里了。我以前从未遇到过这个错误,我正试图找出如何修复它。我目前正在尝试在我的oop类中创建类C++ 如何修复包含必须返回值的错误?,c++,C++,只是一个简单的问题,我不知道为什么会出现这个错误或者如何修复它 错误是C4176 Box::getParameters:必须返回一个值。它说它必须返回值,但我确实把getParameter放在那里了。我以前从未遇到过这个错误,我正试图找出如何修复它。我目前正在尝试在我的oop类中创建类 class Box { public: double height;//length of the box double depth;//depth of the box double w
class Box
{
public:
double height;//length of the box
double depth;//depth of the box
double width;//width of the box
//Declare Member functions
double getVolume(void);
string getParameters(void);
//Mutators
void setHeight(double height);
void setWidth(double width);
void setDepth(double depth);
//Accessors
double getDepth(double depth);
double getWidth(double width);
double getHeight(double height);
}; //end class
//member function definitions
double Box::getVolume(void)//get volume will cal and output the volume when called
{
return height * width * depth;
}
void Box::setDepth(double depth)
{
depth = 0.01;
}
double Box::getDepth(double depth)
{
return depth;
}
void Box::setWidth(double width)
{
width = 0.01;
}
double Box::getWidth(double width)
{
return width;
}
void Box::setHeight(double height)
{
height = 0.01;
}
double Box::getHeight(double height)
{
return height;
}
string Box::getParameters(void)
{
cout << "\nDepth:" << getDepth(depth) << endl <<
"Width:" << getWidth(width) << endl <<
"Height :" << getHeight(height) << endl;
}
int main()
{
double depth;
double width;
double height;
double volume;
Box box;
cout << "Please Enter a Length for your box: ";
cin >> depth;
box.setDepth(depth);
cout << "Please Enter a Width for your box: ";
cin >> width;
box.setWidth(width);
cout << "Please Enter a Height for your box: ";
cin >> height;
box.setHeight(height);
//cout << "\n\n" << box.getParameters();
volume = box.getVolume();
cout << "\n\nThe Volume of your box is: " << volume;
box.getParameters();
return 0;
}
类框
{
公众:
双倍高度;//长方体的长度
双深度;//长方体的深度
双宽度;//框的宽度
//声明成员函数
双卷(无效);
字符串getParameters(void);
//突变子
空隙设置高度(双倍高度);
空隙宽度(双倍宽度);
空隙设置深度(双深度);
//访问者
双深度(双深度);
双倍宽度(双倍宽度);
双高(双高);
}; //末级
//成员函数定义
double Box::getVolume(void)//get volume将在调用时校准并输出卷
{
返回高度*宽度*深度;
}
空盒::设置深度(双深度)
{
深度=0.01;
}
双框::getDepth(双深度)
{
返回深度;
}
空框::设置宽度(双倍宽度)
{
宽度=0.01;
}
双框::getWidth(双宽度)
{
返回宽度;
}
空盒::设置高度(双倍高度)
{
高度=0.01;
}
双框::getHeight(双高)
{
返回高度;
}
字符串框::getParameters(void)
{
cout保持更改的简单性,使用stringstream而不是stdout,并返回字符串而不是打印到控制台
string Box::getParameters(void)
{
std::stringstream ss;
ss << "\nDepth:" << getDepth(depth) << endl <<
"Width:" << getWidth(width) << endl <<
"Height :" << getHeight(height) << endl;
return ss.str();
}
字符串框::getParameters(void)
{
std::stringstream-ss;
你班上有三种不同的方法
从类中返回值的第一个访问器(也称为getter),这些访问器应该没有参数,并且对于它们返回的任何值都有一个返回类型,例如
// get the height of the box
double Box::getHeight()
{
return height;
}
// set the height of the box to h
void Box::setHeight(double h)
{
height = h;
}
更改类中的值的mutator(又称setter),它们应该有一个参数,即类的新值和返回类型void
(即它们不返回任何内容)
// get the height of the box
double Box::getHeight()
{
return height;
}
// set the height of the box to h
void Box::setHeight(double h)
{
height = h;
}
最后还有一些方法既不是访问器也不是变异器,你的getParameters
方法就是其中之一。关于这些方法没有硬性的规则,只是取决于它们做什么。你的getParameters
方法实际上打印一些东西,所以getParameters
是个坏名字,应该称之为printparameterters
。由于它所做的只是打印一些内容,因此不应返回任何内容,也不需要任何参数。下面是如何编写它
// print the box parameters
void Box::printParameters()
{
cout << "\nDepth:" << getDepth() << endl <<
"Width:" << getWidth() << endl <<
"Height :" << getHeight() << endl;
}
//打印框参数
void Box::printParameters()
{
您是否需要在编译器抱怨的函数中的所有可能分支上返回一个值。即在字符串框::getParameters(void)
中,cout
语句只在终端上打印文本,不返回任何内容。您已经这样声明了函数:字符串框::getParameters(void)
这意味着它必须返回字符串。你声明了你的函数getParameters
来返回std::string
,但是你没有遵守这个承诺。你的实现没有返回任何东西。要么使该函数无效或者返回一个std::string
类型的对象。我不这样做当作void@JessicaSimcoestringbox::getParameters(void)
,返回类型是string
。如果它是void
,它会说void Box::getParameters(void)
。这就是问题的全部,您声明了一个函数,该函数应返回void
作为返回string
,这就是错误的原因。我确信您有理由将string
放在那里,但我不确定是什么。它不允许我执行void box::getParameters()它给了我太多的错误声明与box::getParameters不兼容,不允许不完整的类型,函数调用中的参数太少,函数调用中的参数太少,函数中的参数太少call@JessicaSimcoe当您将返回类型更改为void
时,必须在类和在定义中,这就是为什么它说它是不兼容的,因为您只在一个地方更改了它。类框{…void getParameters();…};…void box::getParameters(){…}
请在这两个位置查看?void
。请查看您复制的代码中的getter函数。您本可以解决这一困惑,而不是将其永久化。