为什么我的对象向量中的元素在调用对象成员函数之一时没有更新? [英] Why is my elements in my vector of objects not updating upon calling one of the objects member function?

查看:28
本文介绍了为什么我的对象向量中的元素在调用对象成员函数之一时没有更新?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我一直在尝试使用 Visual Studio 调试器进行调试,但我不明白为什么我的函数 emp.setHoursWorked(hWorked);recordHoursWorkedForEmployeecode> 似乎只是更新 numOfHoursWorked 而在 recordHoursWorkedForEmployee 中,一旦程序退出该函数,向量中所有员工的 numOfHoursWorked 就会返回到 0.下面是有问题的代码.任何帮助将不胜感激.

I have been trying to do debug this for a long time using visual studio debugger but I can't figure out why my function emp.setHoursWorked(hWorked); in recordHoursWorkedForEmployee only seems to be update numOfHoursWorked while in recordHoursWorkedForEmployee, as soon the program exits the function the numOfHoursWorked of all Employees in the vector go back to 0. Below is the code in question. Any help would be appreciated.

#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 中,但任何更改都不会反映在原始对象中.

The variable emp is a copy of the object in the container. So if you update this you are only updating the copy. Each iteration you get a new value copied into emp but any changes are not reflected in the original object.

您可能的意思是:

for (Employee& emp : companyEmployees)
            ^^^

这里的 emp 是对向量内部对象的引用.如果你修改它,你就是在修改向量中的原始值.

Here emp is a reference to the object inside the vector. If you modify this you are modifying the original value inside the vector.

这篇关于为什么我的对象向量中的元素在调用对象成员函数之一时没有更新?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆