Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
537 views
in Technique[技术] by (71.8m points)

c++ - Why is my elements in my vector of objects not updating upon calling one of the objects member function?

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;
        }
    }

}

I excluded the header file here in order to not paste too many things not relevant to the problem i'm facing, only implementations of member functions relevant to the problem provided

//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;
}
See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

This line makes a copy of each employee:

for (Employee emp : companyEmployees)

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.

You probably meant:

for (Employee& emp : companyEmployees)
            ^^^

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


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

2.1m questions

2.1m answers

60 comments

57.0k users

...