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-我相信这不是您所期望的

  • 通常,如果函数返回某个内容,请将其分配给变量,如图2所示否则它将不会做任何有用的事情,您将无法重用它


  • 例如在这句话中

    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();//圆括号表示“调用函数,运行代码”
    浮动天数=读取天数();
    总浮动=平均*天;
    
    你不能把一个函数传递给一个期望浮点的对象吗。这不比编译器告诉你的多。使用更好的变量和函数名-并避免重载使用这样的名称会让这个错误更明显哦我明白了。。我过于关注标题中的错误:|