我的目标是将数组传递给函数,但出现以下错误:
error: cannot convert ‘float’ to ‘float*’ for argument ‘9’ to ‘void calc_fun(float*, float*, float*, float*, float*, float*, float*, float*, float*, float*, int)’ calc_fun(rate, hrs_worked, gross, 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);` -> no matching functions for call to "calc_fun" 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)` -> is never used, I don't understand this one as it's being used in `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;
}
}
答案 0 :(得分:1)
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
作为声明为指针的参数传递。类型不匹配。