c++ - 参数传递转换 float 错误

标签 c++ function parameter-passing

我在为这个简单程序传递参数时遇到问题。尝试编译此程序时出现“无法将‘float*’转换为‘float’参数‘2’到‘void getData(std::string, float, float, float, float)’”错误。谁能弄清楚我做错了什么?我一直在努力。注意:请忽略已弃用的内容,例如 system("PAUSE") 和其他一些内容。这是我的老师认为我编码的简单方式,这就是他希望我在这个程序中使用的方式。我知道 getchar() 并将其用于练习和最终工作。另外,这应该不会影响程序,因为我之前在我的 C++ 类的小程序上使用它没有问题。

代码如下:

#include<iostream>
#include<string>
#include<stdlib.h>
using namespace std;

void getData(string,float,float,float,float);
void getCalc(int,float,float,float,float,float,float,float,float,float,float);
void getPrint(float,float,float);

int main()
{
 int const acres=1000;
 string crop;
 float cpa[4];
 float yield[4];
 float per[4];
 float increase[4];
 float cost[4];
 float grossmin[4];
 float grossmax[4];
 float netmin[4];
 float netmax[4];
 float netave[4];
 getData(crop,cpa,yield,per,increase);
 getCalc(acres,cpa,yield,per,increase,cost,grossmin,grossmax,netmin,netmax,netave);
 getPrint(netmin,netmax,netave);
 system("PAUSE");
 return 0;
}

void getData(string fcrop,float fcpa[],float fyield[],float fper[],float fincrease[])
{
 for (int i=0;i<4;i++)
 {
  cout<<"Enter the crop: ";
  getline(cin,fcrop);
  cout<<"Enter the cost per acre:$ ";
  cin>>fcpa[i];
  cout<<"Enter the yield: ";
  cin>>fyield[i];
  cout<<"Enter $/bishell: ";
  cin>>fper[i];
  cout<<"Enter the percentage increase: ";
  cin>>fincrease[i];
  cin.ignore(80,'\n');
 }
}

void getCalc(int acres,float fcpa[],float fyield[],float fper[],float fincrease[],float fcost[],float fgrossmin[],float fgrossmax[],float fnetmin[],float fnetmax[],float fnetave[])
{
 for (int i=0;i<4;i++)
 {
  int acres=1000;
  fcost[i]=acres*fcpa[i];
  fgrossmin[i]=acres*fyield[i]*fper[i];
  fgrossmax[i]=fgrossmin[i]+(fgrossmin[i]*fincrease[i]/100);
  fnetmin[i]=fgrossmin[i]-fcost[i];
  fnetmax[i]=fgrossmax[i]-fcost[i];
  fnetave[i]=(fnetmin[i]+fnetmax[i])/2;
 }
}

void getPrint(float fnetmin[],float fnetmax[],float fnetave[])
{
 for (int i=0;i<4;i++)
 {
  cout<<"The minumum profit is:$ "<<fnetmin[i]<<endl;
  cout<<"The maximum profit is:$ "<<fnetmax[i]<<endl;
  cout<<"The average profit is:$ "<<fnetave[i]<<endl;
 }
}

最佳答案

在程序开始时你写的原型(prototype)中,是这样写的。

void getData(string,float,float,float,float) ;

它应该是这个而不是与其定义中的相同。

void getData(string,float[],float[],float[],float[]);

函数原型(prototype)在声明和实现上应该相同。

关于c++ - 参数传递转换 float 错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/12323299/

相关文章:

python - Python 中的多元微分

python - 我应该在函数调用的最后一个参数之后添加一个逗号吗?

c++ - 在 C 和 C++ 中传递二维数组的比较

c++ - 返回一个参数被评估的函数

c++ - R 值插入不适用于 unordered_map

c++ - 通过引用传递 3-Dim 固定长度数组

javascript - 为什么这个 javascript 函数不从 for 循环返回多个对象?

qt - 通过 Qt 中的信号/槽设置传递类

c++ - 生成英文字母表中的字母

c++ - 互斥体解锁时解除定时接收阻止