C++ 错误:无法转换'void(*)(';到`浮动';
我在将C++ 错误:无法转换'void(*)(';到`浮动';,c++,function,compiler-errors,syntax-error,C++,Function,Compiler Errors,Syntax Error,我在将void(*)(转换为float时遇到问题。这是我的密码: #include <iostream> using namespace std; void instructions(); void numDays(); void med(); void sur(); void lab(); float room(int days); float misc(float meds, float surgical, float lab); float total(float room
void(*)(
转换为float
时遇到问题。这是我的密码:
#include <iostream>
using namespace std;
void instructions();
void numDays();
void med();
void sur();
void lab();
float room(int days);
float misc(float meds, float surgical, float lab);
float total(float room, float misc);
int main()
{
int days;
float meds;
float surgical;
instructions();
numDays();
med();
sur();
lab();
room( days);
misc(meds, surgical, lab);
total( room, misc);
}
void instructions()
{
cout<<"this program will ask you some questions. After that it will print out all of the users charges and the tital\
amount that is owed. "<<endl;
}
void numDays()
{
float days;
cout<<"Enter in users length of stay. "<<endl;
cin>> days;
}
void med()
{
float meds;
cout<<"Enter int the users medication charges. "<<endl;
cin>> meds;
}
void sur()
{
float surgical;
cout<<"Enter in the users surgical charges. "<<endl;
cin>> surgical;
}
void lab()
{
float lab;
cout<<"Enter in the users lab fees. "<<endl;
cin>> lab;
}
float room(float days)
{
float room;
room = (days*450);
}
float misc(float meds, float surgical, float lab)
{
float misc;
misc = (meds + surgical + lab);
return 0;
}
float total(float room, float misc)
{
float total;
total = (room + misc);
return total;
}
room()
函数中,我假定您忘记了返回语句房间
和杂项
功能的结果。如果你这样做的话,这就行了
float misc = misc(meds, surgical, lab);
float room = room(days);
float total = total(room, misc);
main
中的变量不同。您可以使这些函数返回输入的结果,然后执行float lab=lab()
misc
函数总是返回0-我相信这不是您所期望的例如在这句话中
misc(meds, surgical, lab);
参数实验室是一个函数指针。我没有看你的代码,但也许你是说
misc(meds, surgical, lab());
然而,即使在这种情况下,您也会得到一个错误,因为function lab具有返回类型void。
看来你不明白自己在做什么。好的,让我们从头开始。像您的示例一样的计算机程序由两个不同的部分组成:
med
。这将导致混乱。因此,典型的样式指南建议函数名在其名称中使用动词,例如read\u med
,这样您就知道函数应该做什么。另一方面,变量名通常是名词,如med
,days
,total
该函数如下所示:
float read_med() { // this is a function (active part)
float med; // and this is a variable (passive part)
cout << "Enter the medication amount: ";
cin >> med;
return med;
}
float med = read_med(); // the round parentheses mean “call the function, run the code”
float days = read_days();
float total = med * days;
cout << "Total: " << total << endl;
float read\u med(){//这是一个函数(活动部件)
float-med;//这是一个变量(被动部分)
cout>med;
返回医学院;
}
然后可以像这样调用此函数:
float read_med() { // this is a function (active part)
float med; // and this is a variable (passive part)
cout << "Enter the medication amount: ";
cin >> med;
return med;
}
float med = read_med(); // the round parentheses mean “call the function, run the code”
float days = read_days();
float total = med * days;
cout << "Total: " << total << endl;
float-med=read_-med();//圆括号表示“调用函数,运行代码”
浮动天数=读取天数();
总浮动=平均*天;
你不能把一个函数传递给一个期望浮点的对象吗。这不比编译器告诉你的多。使用更好的变量和函数名-并避免重载使用这样的名称会让这个错误更明显哦我明白了。。我过于关注标题中的错误:|