我的目标是将数组传递给函数,但出现以下错误:
错误:无法将参数 '9' 的 'float' 转换为 'float*' 到 'void calc_fun(float*, float*, float*, float*, float*, float*, float*, float*, float*, float *, int)' calc_fun(rate, hrs_worked, total, overtime, state_tax,fed_tax, uni_fee, net, total_gross, avg_gross, SIZE);
而且我真的不明白为什么,因为我认为它遵循与void input_fun(...)
似乎工作得很好的相同步骤。
IDE 显示的其他错误,这可能会有所帮助。(不在调试器中,我使用 CLion):
calc_fun(rate, hrs_worked,gross, overtime, state_tax, fed_tax, uni_fee, net, total_gross, avg_gross, SIZE);` -> 没有匹配的函数调用“calc_fun” void calc_fun(float rate[], float hrs[],浮动总[],浮动公开[],浮动st_tx[],浮动fed_tx[],浮动uni_fee[],浮动净[],浮动tgross,浮动agross,const int S)`->从未使用过,我不使用不理解这个,因为它在`int main`中使用。
任何帮助将不胜感激,谢谢。
#include <iostream>
#include <iomanip>
#include <string>
#include <cmath>
using namespace std;
const float OVERTIME_R = 1.5,
STATE_TAX = 0.06,
FED_TAX = 0.12,
UNION_FEE = 0.02;
const int SIZE = 10;
typedef string INFO;
void input_fun(INFO[], INFO[], INFO[], INFO[], float[], float[], const int);
void calc_fun(float[], float[], float[], float[], float[], float[], float[], float[], float[], float[], const int);
int main()
{
INFO f_name[SIZE],
m_name[SIZE],
l_name[SIZE],
ID[SIZE];
float rate[SIZE],
hrs_worked[SIZE],
gross[SIZE],
overtime[SIZE],
state_tax[SIZE],
fed_tax[SIZE],
uni_fee[SIZE],
net[SIZE],
total_gross = 0,
avg_gross = 0;
input_fun(f_name, m_name, l_name, ID, rate, hrs_worked, SIZE);
calc_fun(rate, hrs_worked, gross, overtime, state_tax,fed_tax, uni_fee, net, total_gross, avg_gross, SIZE);
return 0;
}
void input_fun(INFO first[], INFO mid[], INFO last[], INFO id[], float payrate[], float hrs[], const int S)
{
for(int i = 0; i < S; i++)
{
cout << "Enter first name of employee "<<i+1<<" : " << endl;
cin >> first[i];
cout << "Enter middle name of employee "<<i+1<<" : " << endl;
cin >> mid[i];
cout << "Enter last name of employee "<<i+1<<" : " << endl;
cin >> last[i];
cout << "Enter the ID of employee "<<i+1<<" : " << endl;
cin >> id[i];
cout << "Pay rate of employee " << i+1 << ": " << endl;
cin >> payrate[i];
if(payrate[i] < 0 || payrate[i] > 50)
{
while(payrate[i] < 0 || payrate[i] > 50)
{
cout << "Must be between 0 and 50: " << endl;
cin >> payrate[i];
}
}
cout << "Hours of work by employee " << i+1 << ": " << endl;
cin >> hrs[i];
if(hrs[i] < 0 || hrs[i] > 60)
{
while(hrs[i] < 0 || hrs[i] > 60)
{
cout << "Must be between 0 and 60: " << endl;
cin >> hrs[i];
}
}
}
}
void calc_fun(float rate[], float hrs[], float gross[], float overt[], float st_tx[], float fed_tx[], float uni_fee[],
float net[], float tgross, float agross, const int S){
for (int i = 0; i < S; i++) {
if (hrs[i] > 40) {
overt[i] = (hrs[i] - 40.0) * rate[i] * OVERTIME_R;
}
gross[i] = (rate[i] * hrs[i]) + overt[i];
st_tx[i] = gross[i] * STATE_TAX;
fed_tx[i] = gross[i] * FED_TAX;
uni_fee[i] = gross[i] * UNION_FEE;
net[i] = gross[i] - (st_tx[i] + fed_tx[i] + uni_fee[i]);
tgross += gross[i];
agross = tgross / S;
}
}
void calc_fun(float[], float[], float[], float[], float[], float[], float[], float[], float[], float[], const int); // scroll right ^^^^^^^
您已将此参数声明为指针。
float /* snip */ total_gross = 0,
您已声明total_gross
为float
. 它不是数组,也不是指针。
calc_fun(rate, hrs_worked, gross, overtime, state_tax,fed_tax, uni_fee, net, total_gross, avg_gross, SIZE); // scroll right ^^^^^^^^^^^
你total_gross
作为一个参数传递,你声明为一个指针。类型不匹配。
本文收集自互联网,转载请注明来源。
如有侵权,请联系[email protected] 删除。
我来说两句