【问题标题】:error: cannot convert ‘float’ to ‘float*’ for argument [closed]错误:无法将参数的“float”转换为“float*”[关闭]
【发布时间】:2019-03-22 03:07:57
【问题描述】:

我的目标是将数组传递给函数,但出现以下错误:

错误:无法将参数 '9' 的 'float' 转换为 'float*' 到 'void calc_fun(float*, float*, float*, float*, float*, float*, float*, float*, float*, float *,整数)' 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);` -> 没有匹配函数调用“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)` -> 从未使用过,我不明白这个,因为它在`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;

    }
}

【问题讨论】:

  • 编译器的错误信息很清楚。第 9 个参数应该是 float*。您正在传递float
  • @RSahu 但是为什么应该是float*。它与其他论点有何不同。
  • 函数有 9 个参数,您可能需要考虑将数据聚合到一个结构中并拥有该结构的单个数组。附带的好处可能是巨大的。
  • @user4581301 我希望我可以,但是教授不允许这个作业,因为我们还没有检查结构。
  • @DavidGordeladze -- 查看你的函数声明。您将其编写为将第 9 个参数设为 float *。此外,将float [] 指定为参数与指定float * 没有区别。

标签: c++ arrays function


【解决方案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 作为您声明为指针的参数传递。类型不匹配。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2018-05-24
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多