【问题标题】:C++ Object Function Error MessageC++ 对象函数错误消息
【发布时间】:2016-01-18 04:01:27
【问题描述】:

好的,所以我完成了我的程序,该程序基本上只是计算 3 名员工的工作时间,没什么太复杂的。但是,现在我完成了,我收到两条错误消息。第一个说:

表达式必须是可修改的左值。

这里指的是x.hoursAddsomethingup函数下的使用。第二条错误消息说:

'=':左操作数必须是左值。

这也说明它与Addsomethingup 函数下的iTotal_hours 行有关。我对使用 C++ 的类不太熟悉,所以如果有人能提供一些建议,我将不胜感激。

#include "stdafx.h"
#include <iostream>
#include <string>
#include <iomanip>

using namespace std;


class EmployeeClass {
public:
    string EmployeeName;
    int hours;
    float wage;
    float basepay;
    float salary;

    int overtime_hours;
    float overtime_pay;
    float overtime_extra;

    float iTotal_salaries;
    float iIndividualSalary;
    int iTotal_hours;
    int iTotal_OvertimeHours;

    void ImplementCalculations() {
        overtime_hours = 0;
        overtime_pay = 0;
        overtime_extra = 0;

        if (hours > 40) {
            overtime_hours = hours - 40;
        }
        else {
            overtime_hours = 0;
        }

        basepay = 40 * wage;
        overtime_pay = wage * 1.5;
        overtime_extra = overtime_pay * overtime_hours;
        salary = overtime_extra + basepay;
    }

    void DisplayEmployInformation(void) {       
        cout << "Employee Name ............. = " << EmployeeName << endl <<
            "Base Pay .................. = " << basepay << endl <<
            "Hours in Overtime ......... = " << overtime_hours << endl <<
            "Overtime Pay Amount........ = " << overtime_pay << endl <<
            "Total Pay ................. = " << salary << endl;
    }

    void Addsomethingup(EmployeeClass x, EmployeeClass y, EmployeeClass z) {
        iTotal_salaries = 0;
        iTotal_hours = 0;
        iTotal_OvertimeHours = 0;

        iTotal_salaries = x.wage + y.wage + z.wage;
        iTotal_hours = x.hours + y.hours = z.hours;
        iTotal_OvertimeHours = x.overtime_hours + y.overtime_hours + z.overtime_hours;

        cout << " %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%\n" <<
            "%%%% EMPLOYEE SUMMARY DATA%%%%%%%%%%%%%%%%%%%%%%%\n" <<
            "%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%\n" <<
            "%%%% Total Employee Salaries ..... = " << iTotal_salaries << endl <<
            "%%%% Total Employee Hours ........ = " << iTotal_hours << endl <<
            "%%%% Total Overtime Hours......... = " << iTotal_OvertimeHours << endl <<
            "%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%\n" <<
            "%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%" << endl;
    }
};

int main()
{
    system("cls");

    cout << "\nWelcome to the Employee Pay Center\n\n";

    EmployeeClass firstEmployee;
    EmployeeClass secondEmployee;
    EmployeeClass thirdEmployee;
    EmployeeClass totalEmployee;

    cout << "Please enter the first Employees name: \n";                                //First employees input
    cin >> firstEmployee.EmployeeName;
    cout << "Please enter " << firstEmployee.EmployeeName << "'s hours worked: \n";
    cin >> firstEmployee.hours;
    cout << "Please enter " << firstEmployee.EmployeeName << "'s hourly wage; \n\n";
    cin >> firstEmployee.wage;


    cout << "Please enter the second Employees name: \n";                               //Second emlpoyee input
    cin >> secondEmployee.EmployeeName;
    cout << "Please enter " << secondEmployee.EmployeeName << "'s hours worked: \n";
    cin >> secondEmployee.hours;
    cout << "Please enter " << secondEmployee.EmployeeName << "'s hourly wage; \n\n";
    cin >> secondEmployee.wage;

    cout << "Please enter the third Employees name: \n";                                //Third employees input
    cin >> thirdEmployee.EmployeeName;
    cout << "Please enter " << thirdEmployee.EmployeeName << "'s hours worked: \n";
    cin >> thirdEmployee.hours;
    cout << "Please enter " << thirdEmployee.EmployeeName << "'s hourly wage; \n\n";
    cin >> thirdEmployee.wage;

    firstEmployee.ImplementCalculations();
    secondEmployee.ImplementCalculations();
    thirdEmployee.ImplementCalculations();

    totalEmployee.Addsomethingup(firstEmployee, secondEmployee, thirdEmployee);

}

【问题讨论】:

    标签: c++ function object


    【解决方案1】:

    你的问题是

    iTotal_hours = x.hours + y.hours = z.hours;
    

    operator precendece 规定 x.hours + y.hours 发生在 y.hours = z.hours 之前,所以实际发生的是

    iTotal_hours = (x.hours + y.hours) = z.hours;
    

    这不起作用,因为(x.hours + y.hours) 创建了一个无法分配的临时对象。

    我认为你的意思是 + 而不是 = 会成功

    iTotal_hours = x.hours + y.hours + z.hours;
    

    这与您为 iTotal_salariesiTotal_OvertimeHours 所做的一致

    【讨论】:

    • 哦,有时我会犯很多简单的错误。非常感谢您提供快速且布局合理的回复,非常感谢。
    • @JordanLeeBurnes 没问题。很高兴能提供帮助。
    猜你喜欢
    • 1970-01-01
    • 2019-06-27
    • 1970-01-01
    • 1970-01-01
    • 2023-03-10
    • 2012-05-16
    • 2015-02-13
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多