C++对象函数错误消息
C++ Object Function Error Message
好吧,所以我完成了我的程序,基本上只是计算 3 名员工的工作时间,没有什么太复杂的。但是,现在我已完成,我收到两条错误消息。第一个说:
表达式必须是可修改的左值。
这是指在Addsomethingup
功能下使用x.hours
。第二条错误消息说:
'=':左操作数必须是 L 值。
这也说明它与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 Centernn";
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; nn";
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; nn";
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; nn";
cin >> thirdEmployee.wage;
firstEmployee.ImplementCalculations();
secondEmployee.ImplementCalculations();
thirdEmployee.ImplementCalculations();
totalEmployee.Addsomethingup(firstEmployee, secondEmployee, thirdEmployee);
}
您的问题与
iTotal_hours = x.hours + y.hours = z.hours;
运算符先验规定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_salaries
和iTotal_OvertimeHours
所做的是一致的
相关文章:
- boost::进程间消息队列引发错误
- C++错误消息*成员参考.**初学者*
- 如何通过参数抛出错误消息
- glad 导致 glfwSwapBuffers 返回错误消息
- FindPackageHandleStandardArgs.cmake:137 的 CMake 错误(消息):找不到 Boost (缺少:正则表达式)(找到合适的版本"1.72.0",
- 如何接受 [ENTER] 键作为无效输入并发送错误消息
- 重新定义预定义的 errno 错误消息 (E2BIG)
- SDL 映像:无法打开映像,仅显示错误消息
- 错误消息:使用"string* +="后"no match for 'operator+='"
- 使用公钥加密消息:BER 解码错误
- 我在主函数的左括号上不断收到错误,消息为obj\Debug\main.o||在函数"ZN11linked_listC1Ev"中:|
- 错误消息"expected expression"....有人知道它为什么这么说吗?
- 在 Eclipse: "error: no match for 'operator='" 中获取错误消息
- 出现分段错误时,更改显示的消息错误
- 使用C 中的OPENSL RSA函数解密消息错误
- Linux汇编程序消息错误c++
- C++写入消息错误
- 发送http post消息错误报告的安全方式
- C++,在尝试创建类时收到此消息:错误:没有在类“media”中声明的“void media::*()”成员函数
- 编译器错误消息错误:令牌之前的预期')' '*'。我的构造函数出错