使用push_back的C ++ Vector结构

时间:2015-11-02 03:54:51

标签: c++ vector struct push-back

这是我遇到问题的课程作业。到目前为止,我认为我对C ++中的向量非常熟悉。该程序应该从用户那里获取一个文件来计算用户付费,然后在一张漂亮的表中吐出所有相关信息。

它必须包含struct的向量,我必须使用push_back。我得到了两个我目前无法弄清楚的错误。在main()末尾的for循环中,它告诉我无法使用Employee初始化向量的引用类型。病房后面的两个函数告诉我,例如.HoursWorked不是结构的成员。

我试着寻找其他问题寻求帮助,但他们都提到不使用指针,我100%肯定我的程序中没有指针。我用来测试程序的txt文件如下所示:

  

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

代码:

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

2 个答案:

答案 0 :(得分:1)

至少......你有这条线:

var result = dNameProps.Select(o => Tuple.Create(o.Key, o.Value)).ToArray();

很明显,我们都调用了一个函数 calculatePay(buisness[index]); ,然后我们将它传递给calculatePay

但是你的函数原型说它需要Employee。您可能希望功能取代std::vector<Employee>

答案 1 :(得分:1)

您应该使用要放入的元素调用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’在调用方法calculatePay()的行上。我们可以推断您已将Employee传递给了vector Employee作为参数的函数。您可以通过将calculatePay(vector<Employee>& buisness)更改为calculatePay(Employee& buisness)来解决此问题。这将解决error: ‘class std::vector<Employee, std::allocator<Employee> >’ has no member named ‘HoursWorked’