为什么我的对象向量中的元素在调用对象成员函数之一时没有更新?
Why is my elements in my vector of objects not updating upon calling one of the objects member function?
我已经尝试使用 visual studio 调试器对此进行调试很长时间了,但我无法弄清楚为什么 recordHoursWorkedForEmployee
中的函数 emp.setHoursWorked(hWorked);
似乎只是更新numOfHoursWorked
而在 recordHoursWorkedForEmployee
中,一旦程序退出函数,向量中所有 Employees 的 numOfHoursWorked
就会返回 0。下面是有问题的代码。任何帮助将不胜感激。
#ifndef PAYROLLSYSTEM
#define PAYROLLSYSTEM
#include "Employee.h"
#include "Paycheck.h"
#include <string>
#include <vector>
using namespace std;
class PayRollSystem
{
public:
//custom constructor gets company name
PayRollSystem(string);
void createEmployee(string, string,string, double);
void removeEmployee(string);
void recordHoursWorkedForEmployee(string);
void issuePaychecks();
private:
string companyName;
vector<Employee> companyEmployees;
};
#endif
void PayRollSystem::createEmployee(string empId, string fName, string lName, double hWage)
{
Employee temEmp = Employee(empId, fName, lName, hWage);
companyEmployees.push_back(temEmp);
}
void PayRollSystem::recordHoursWorkedForEmployee(string empId)
{
for (Employee emp : companyEmployees)
{
if (emp.getEmployeeId() == empId)
{
int hWorked = 0;
cout << "What are the hours the worked for " + emp.getEmployeeId() + " during current pay period?" << endl;
cin >> hWorked;
//TODO: For some reason this line is not updating the hours worked. Must fix!
emp.setHoursWorked(hWorked);
cout << "Hours for " + emp.getEmployeeId() + " have been changed to " << emp.getHoursWorked() << endl;
}
}
}
我在这里排除了头文件是为了不粘贴太多与我面临的问题无关的东西,只提供与问题相关的成员函数的实现
//Overloaded constructor to be used with PayRollSystem
Employee::Employee(string empId, string fName, string lName, double hWage)
{
employeeId = empId;
firstName = fName;
lastName = lName;
hourlyWage = hWage;
numOfHoursWorked = 0;
}
void Employee::setHoursWorked(int hWorked)
{
if (hWorked >= 0)
numOfHoursWorked = hWorked;
else
{
cout << "Invalid number of hours worked." << endl;
exit(EXIT_FAILURE);
}
}
string Employee::getEmployeeId() const
{
return employeeId;
}
此行复制每个员工:
for (Employee emp : companyEmployees)
变量emp
是容器中对象的副本。因此,如果您更新它,您只是在更新副本。每次迭代都会将一个新值复制到 emp
,但任何更改都不会反映在原始对象中。
你的意思可能是:
for (Employee& emp : companyEmployees)
^^^
这里emp
是对vector内部对象的引用。如果你修改它,你就是在修改向量中的原始值。
我已经尝试使用 visual studio 调试器对此进行调试很长时间了,但我无法弄清楚为什么 recordHoursWorkedForEmployee
中的函数 emp.setHoursWorked(hWorked);
似乎只是更新numOfHoursWorked
而在 recordHoursWorkedForEmployee
中,一旦程序退出函数,向量中所有 Employees 的 numOfHoursWorked
就会返回 0。下面是有问题的代码。任何帮助将不胜感激。
#ifndef PAYROLLSYSTEM
#define PAYROLLSYSTEM
#include "Employee.h"
#include "Paycheck.h"
#include <string>
#include <vector>
using namespace std;
class PayRollSystem
{
public:
//custom constructor gets company name
PayRollSystem(string);
void createEmployee(string, string,string, double);
void removeEmployee(string);
void recordHoursWorkedForEmployee(string);
void issuePaychecks();
private:
string companyName;
vector<Employee> companyEmployees;
};
#endif
void PayRollSystem::createEmployee(string empId, string fName, string lName, double hWage)
{
Employee temEmp = Employee(empId, fName, lName, hWage);
companyEmployees.push_back(temEmp);
}
void PayRollSystem::recordHoursWorkedForEmployee(string empId)
{
for (Employee emp : companyEmployees)
{
if (emp.getEmployeeId() == empId)
{
int hWorked = 0;
cout << "What are the hours the worked for " + emp.getEmployeeId() + " during current pay period?" << endl;
cin >> hWorked;
//TODO: For some reason this line is not updating the hours worked. Must fix!
emp.setHoursWorked(hWorked);
cout << "Hours for " + emp.getEmployeeId() + " have been changed to " << emp.getHoursWorked() << endl;
}
}
}
我在这里排除了头文件是为了不粘贴太多与我面临的问题无关的东西,只提供与问题相关的成员函数的实现
//Overloaded constructor to be used with PayRollSystem
Employee::Employee(string empId, string fName, string lName, double hWage)
{
employeeId = empId;
firstName = fName;
lastName = lName;
hourlyWage = hWage;
numOfHoursWorked = 0;
}
void Employee::setHoursWorked(int hWorked)
{
if (hWorked >= 0)
numOfHoursWorked = hWorked;
else
{
cout << "Invalid number of hours worked." << endl;
exit(EXIT_FAILURE);
}
}
string Employee::getEmployeeId() const
{
return employeeId;
}
此行复制每个员工:
for (Employee emp : companyEmployees)
变量emp
是容器中对象的副本。因此,如果您更新它,您只是在更新副本。每次迭代都会将一个新值复制到 emp
,但任何更改都不会反映在原始对象中。
你的意思可能是:
for (Employee& emp : companyEmployees)
^^^
这里emp
是对vector内部对象的引用。如果你修改它,你就是在修改向量中的原始值。