C++中的重载错误[按值传递和按引用传递]

Overloading error in C++ [Pass by Value and Pass by Reference]

我正在从事一个项目,该项目使用 overloading.This 计算两种类型的员工(薪水和小时工)的收入项目有 3 个文件,这里是主要来源:

#include <iostream>
#include "Grosspay.h"
using namespace std;

void Grosspay::determineGrosspay()
{
    cout << "Enter 1 - Calculate payroll for hourly employee" << endl;
    cout << "Enter 2 - Calculate payroll for salary employee" << endl;
    cout << "Enter 3 - Exit" << endl;

    cout << "Federal Tax is 10% of Grosspay" << endl;
    cout << "State Tax is 5% of Grosspay" << endl;
    while (choice != 3)
    {
        cout << "\nEnter your choice: " << endl;
        cin >> choice;
        switch (choice)
        {
        case 1:
            cout << "Enter employee ID: " << endl;
            cin >> ID;
            cout << "Enter hours: " << endl;
            cin >> hours;
            cout << "Enter payrate: " << endl;
            cin >> payrate;

            cout << "Employee ID: %d" << ID << endl;
            cout << "The net pay for hourly employee: %.2f\n" << income(hours, payrate) << endl;
            break;
        case 2:
            cout << "Enter employee ID: " << endl;
            cin >> ID;
            cout << "Enter salary: " << endl;
            cin >> year;

            cout << "The net pay for salaried employee: %.2f\n" << income(year) << endl;
            break;
        case 3:
            cout << "Exited program" << endl;
            break;
        default:
            cout << "Please try again!" << endl;
            break;
        }
    }
}
double income(double hours, double payrate)
{
    double grosspay = 0;
    double federaltax = .10;
    double statetax = .05;
    double totaltax;
    double netpay;
    if (hours <= 40)
    {
        grosspay = payrate * hours;
    }if (hours > 40 && hours <= 50)
    {
        grosspay = (payrate * 40) + ((hours - 40) * payrate * 1.5);
    }
    if (hours > 50)
    {
        grosspay = (payrate * 40) + (10 * payrate * 1.5) + ((hours - 50) * payrate * 2);
    }
    cout << "Grosspay weekly is %.2f\n" << grosspay << endl;

    federaltax = grosspay * .10;
    cout << "Federal Tax is: %.2f\n" << federaltax << endl;

    statetax = grosspay * .05;
    cout << "State Tax is: %.2f\n" << statetax << endl;

    totaltax = federaltax + statetax;
    cout << "Total tax is: %.2f\n" << totaltax << endl;

    netpay = grosspay - totaltax;
    return (netpay);

}
double income(double year)
{
    double grosspay;
    double federaltax = .10;
    double statetax = .05;
    double totaltax;
    double netpay;

    grosspay = year / 52;
    cout << "Grosspay weekly is %.2f\n" << grosspay << endl;

    federaltax = grosspay * .10;
    cout << "Federal Tax is: %.2f\n" << federaltax << endl;

    statetax = grosspay * .05;
    cout << "State Tax is: %.2f\n" << statetax << endl;

    totaltax = federaltax + statetax;
    cout << "Total Tax is: %.2f\n" << totaltax << endl;

    netpay = grosspay - totaltax;
    return (netpay);
}

不知道为什么程序找不到income 来自

cout << "The net pay for hourly employee: %.2f\n" << income(hours, payrate) << endl;
cout << "The net pay for salaried employee: %.2f\n" << income(year) << endl;

另外,我需要在程序中至少使用一个函数来演示传值;至少,其中一个函数可以演示使用引用参数传递引用。我不知道如何把它放在源代码中。有人帮忙吗?

由于 income 函数未在 Grosspay class 中定义,因此应在调用它们之前对其进行定义。尝试将 income 的定义移动到 determineGrosspay 方法之前。

另一种解决方案是通过将原型 "before" 移动到 determineGrosspay 函数及其之后的实现来使用 function prototype


既然你在评论中询问了按引用传递和按值传递,我将把它包含在这个答案中。您为 income 函数编写的内容是按值传递的。这意味着传递给函数的变量 is/are 的副本。

通过引用传递意味着您将变量的引用传递给函数而不是它的副本。按引用传递假设您的应用程序具有以下函数签名:income(const double &year) 如果您不想修改函数内的年份变量,income(double &year) 如果您想修改 year 的值.


为您的应用程序使用 double income(const double &year)

请研究这意味着什么,因为它是语言的重要组成部分。