eric1379
eric1379

Reputation: 43

Trying to add objects to vector and display the results but its not working getting a seg fault

I am trying to add my objects added on the heap to my vector but i cannot seem to get them to add, calculate and display the results. I am getting a segmen fault when I display the results. I want the objects to be added to the vector then I want to calculate the pay and display the results. What am I doing wrong to cause the seg fault?

Main file

#include <cstdlib>
#include <vector>
#include "Employee.h"

using namespace std;


int main(int argc, char** argv) {
    vector<Employee *> myVector(4);
    Employee *emp1 = new Employee("Joe", 15.0, 45.0);
    Employee *emp2 = new Employee("Sally", 25.0, 35.0);
    Employee *emp3 = new Employee("Mary", 32.0, 55.0);
    Employee *emp4 = new Employee("John", 18.0, 32.0);
    
    myVector.push_back(emp1);
    myVector.push_back(emp2);
    myVector.push_back(emp3);
    myVector.push_back(emp4);
    
    for (int i = 0; i <= myVector.size() - 1; i++)
    {
        myVector[i]->calculatePay();
        myVector[i]->display();
    }
    

    return 0;
}

Header file

#include <iostream>

using namespace std;
#ifndef EMPLOYEE_H
#define EMPLOYEE_H

class Employee {
public:
    string name;
    double payRate;
    double hours;
    double gross;
    Employee * next;
    
    Employee();
    Employee(string name, double payRate, double hours);
    void display();
    void calculatePay();
    Employee(const Employee& orig);
    virtual ~Employee();
private:

};

#endif /* EMPLOYEE_H */

Source File

#include <iomanip>
#include "Employee.h"

Employee::Employee() {
    
    name = "";
    payRate = 0.0;
    hours = 0.0;
    gross = 0.0;
    
    
}
Employee::Employee(string name, double payRate, double hours)
{
    this->name = name;
    this->payRate = payRate;
    this->hours = hours;
}
void Employee::display()
{
    cout << setprecision(2) << fixed << endl;
    cout << "Name: " << name << endl;
    cout << "Pay Rate: " << payRate << endl;
    cout << "Hours: "  << hours << endl;
    cout << "Gross: "  << gross << endl;
}
void Employee::calculatePay()
{
    if (hours > 40)
    {
        gross = ((hours - 40) * payRate * 1.5) + (40 * payRate);
    }
    else
    {
        gross = hours * payRate;
    }
}

Employee::Employee(const Employee& orig) {
}

Employee::~Employee() {
}

Upvotes: 0

Views: 50

Answers (1)

MikeCAT
MikeCAT

Reputation: 75062

The declaration of vector

vector<Employee *> myVector(4);

is telling it to allocate 4 elements. The elements are initialized to nullptr. Using push_back() will add elements after the null elements.

After that, at the line

myVector[i]->calculatePay();

the nullptr is dereferenced and lead to Segmentation Fault.

You should remove the argument for the constructor of vector to create an empty vector. If you want to reserve space for push_back() beforehand, you should use reserve()

vector<Employee *> myVector;
myVector.reserve(4);

Also the loop

for (int i = 0; i <= myVector.size() - 1; i++)

is not bad (for this case that the vector is not empty), but not in the usual way. It should be:

for (size_t i = 0; i < myVector.size(); i++)

Upvotes: 6

Related Questions