簡體   English   中英

使用push_back的Struct C ++矢量

[英]C++ Vector of Struct using push_back

這是我遇到問題的課程的一項作業。 直到現在,我還以為我對C ++中的向量相當熟悉。 該程序應該從用戶那里獲取一個文件,計算出用戶支付的費用,然后將所有相關信息吐出到漂亮的表中。

它必須包含struct的向量,並且我必須使用push_back。 我有兩個錯誤,目前無法確定。 在main()末尾的for循環中,它告訴我不能使用Employee初始化矢量的引用類型。 病房之后的兩個函數告訴我,例如,.HoursWorked不是該結構的成員。

我嘗試查看其他問題以尋求幫助,但他們都提到沒有使用指針,我100%肯定我的程序中沒有指針。 我用於測試程序的txt文件如下所示:

約翰·史密斯123-09-8765 9.00 46 F

凱文·阿什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

碼:

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

至少..你有這行:

        calculatePay(buisness[index]);

因此,很明顯,我們都調用了函數calculatePay ,並將其傳遞給Employee

但是您的函數原型說它需要一個std::vector<Employee> 您可能打算讓這些函數采用Employee &

您應該使用要插入的元素調用vector push_back

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

盡管編譯器錯誤日志看起來很乏味,但是您幾乎總是可以從錯誤日志中獲取足夠的信息。 根據gcc 4.2.1進行編譯,錯誤日志顯示: error: invalid initialization of reference of type 'std::vector<Employee, std::allocator<Employee> >&' from expression of type 'Employee'調用行中error: invalid initialization of reference of type 'std::vector<Employee, std::allocator<Employee> >&' from expression of type 'Employee'方法calculatePay() 我們可以推斷出您將Employee傳遞給了想要將Employee vector作為參數的函數。 您可以通過將calculatePay(vector<Employee>& buisness)更改為calculatePay(Employee& buisness)來解決此問題。 這將解決error: 'class std::vector<Employee, std::allocator<Employee> >' has no member named 'HoursWorked'

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM