简体   繁体   中英

C++ Vector of Struct using push_back

This is an assignment for a course that I am having problems with. Until now I thought I was fairly familiar with vectors in C++. This program is supposed to take a file from the user calculate the users pay then spit back out in a nice table all the information relevant.

It must contain a vector of struct and I must use push_back. I get two errors that I cannot figure out at this moment. In the for loop at the end of main() it tells me a reference type of vector cannot be initialized with Employee. The two functions after ward tell me that I for example .HoursWorked is not a member of the struct.

I tried looking around other questions for help but they all mentioned not using pointers which I'm 100% positive there are no pointers in my program. The txt file I am using for testing the program looks as follows:

John Smith 123-09-8765 9.00 46 F

Kevin Ashes 321-09-8444 9.50 40 F

Kim Cares 131-12-1231 11.25 50 P

Trish Dish 141-51-4564 7.52 24 P

Kyle Wader 432-12-9889 5.75 48 F

Code:

    #include <iostream>
    #include <string>
    #include <fstream>
    #include <sstream>
    #include <iomanip>
    #include <vector>

    using namespace std;

    struct Employee
    {
        string name;
        string ssn;
        double hourlyWage;
        double HoursWorked;
        char status;
        double straightTimePay;
        double overTimePay;
        double netPay;
    };

    void calculatePay(vector<Employee>& buisness);

    void displayEmployee(vector<Employee> buisness);

    int main()
    {
        vector<Employee> buisness;
        string fileName, line;
        ifstream inFile;
        stringstream ss;

        cout << "Please input the file name to read: ";
        cin >> fileName;

        inFile.open(fileName);
        if (!inFile)
        {
            cout << "Cannot open file " << fileName << " Aborting!" << endl;
            exit(1);
        }

        int index = 0;
        string firstName, lastName;

        getline(inFile, line);
        while (!inFile.eof())
        {
            if (line.length() > 0)
            {
                ss.clear();
                ss.str(line);
                buisness.push_back;

                ss >> firstName >> lastName;
                buisness[index].name = firstName + " " + lastName;
                ss >> buisness[index].ssn;
                ss >> buisness[index].hourlyWage >> buisness[index].HoursWorked;
                ss >> buisness[index].status;
                index++;
            }

            getline(inFile, line);
        }

        inFile.close();

        cout << "The information of the buisness:" << endl;
        cout << setw(20) << "Name" << setw(15) << "SSN" << setw(12) << "Hourly Wage";
        cout << setw(14) << "Hours Worked" << setw(18) << "Straight Time Pay";
        cout << setw(14) << "Over Time Pay" << setw(6) << "Status" << setw(10) << "Net Pay" << endl;

        for (index = 0; index < buisness.size(); index++)
        {
            calculatePay(buisness[index]);
            displayEmployee(buisness[index]);
        }

        return 0;
    }

    void calculatePay(vector<Employee>& buisness)
    {
        if (buisness.HoursWorked <= 40)
        {
            buisness.straightTimePay = buisness.hourlyWage * buisness.HoursWorked;
            buisness.overTimePay = 0;
        }
        else
        {
            buisness.straightTimePay = buisness.hourlyWage * 40;
            buisness.overTimePay = buisness.hourlyWage * 1.5 * (buisness.HoursWorked - 40);
        }

        buisness.netPay = buisness.straightTimePay + buisness.overTimePay;

        if (buisness.status == 'F')
            buisness.netPay -= 10;
    }

    void displayEmployee(vector<Employee> buisness)
    {
        int precisionSetting = cout.precision();
        long flagSettings = cout.flags();
        cout.setf(ios::fixed | ios::showpoint);
        cout.precision(2);

        cout << setw(20) << buisness.name << setw(15) << buisness.ssn << setw(12) << buisness.hourlyWage;
        cout << setw(14) << buisness.HoursWorked << setw(18) << buisness.straightTimePay;
        cout << setw(14) << buisness.overTimePay << setw(6) << buisness.status << setw(10) << buisness.netPay << endl;

        cout.precision(precisionSetting);
        cout.flags(flagSettings);
    }

At the very least.. You have the line:

        calculatePay(buisness[index]);

So clearly we all calling a function calculatePay and we're passing it an Employee .

But your function prototype says that it takes a std::vector<Employee> . You probably intended for the functions to take Employee & instead.

You should call vector push_back with the element to put in:

Employee employee;
// assign values to employee
ss << employee.ssn;
ss << employee.name;
business.push_back(employee); 

Although the compiler error logs seem tedious, but you can almost always get enough information from the error logs. Compiling under gcc 4.2.1, the error logs says : error: invalid initialization of reference of type 'std::vector<Employee, std::allocator<Employee> >&' from expression of type 'Employee' on the line of calling method calculatePay() . We can infer that you passed Employee to and function which want an vector of Employee as parameter. You can fix this by change calculatePay(vector<Employee>& buisness) to calculatePay(Employee& buisness) . And that will fix error: 'class std::vector<Employee, std::allocator<Employee> >' has no member named 'HoursWorked'

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM