简体   繁体   English

我如何将一个数据类型不同的向量推回去

[英]how can i push back an in into a vector with different data type

I have two classes, one called " OrganisationRecord " and the other called " PayrollProcessing ". 我有两个类,一个叫做“ OrganisationRecord ”,另一个叫做“ PayrollProcessing ”。 " OrganisationRecord " contains the name occupation and department of an employee. OrganisationRecord ”包含雇员的职业和部门名称。

class OrganisationRecord
{
private:
public:
    string name;
    string occupation;
    string department;
};

class PayrollProcessing
{
private:
    ifstream inputfile;
    ofstream outputfile;
    vector<OrganisationRecord> OrganisationRecords;
public:
    void loadOrganisationRecords(string filename);
    void displayEmployeeOfSalaryGTE(double salary);

    //GTE = greater than or equal to
};

within " PayrollProcessing " are two member functions; PayrollProcessing ”中有两个成员函数; " loadOrganisationRecords " and " displayEmployeeOfSalaryGTE " the code for which are: loadOrganisationRecords ”和“ displayEmployeeOfSalaryGTE ”的量,代码是:

void PayrollProcessing::loadOrganisationRecords(string filename)
{
    inputfile.open(ORGANISATIONALRECORDSFILE);

    if (!inputfile)
    {
        cout << "the organisation records file does not exist!" << endl;
        return;
    }
        OrganisationRecord _organisationrecord;
        int employeenumber;

        while (inputfile >> employeenumber)
        {
            inputfile.ignore();
            getline(inputfile, _organisationrecord.name);
            getline(inputfile, _organisationrecord.occupation);
            getline(inputfile, _organisationrecord.department);

            OrganisationRecords.push_back(_organisationrecord);
        }

        inputfile.close();
}

void PayrollProcessing::displayEmployeeOfSalaryGTE(double salary)
    {
        unsigned int count;
        salary = SALARY;

        if (salary < 0)
        {
            cout << "no record match this criteria!" << endl;
            return;
        }
        for (count = 0; count < PayrollRecords.size(); count++)
        {
            if (PayrollRecords[count].salary >= salary)
            {
                cout << "=============================================" << endl;
                cout << "Employeenumber: " << endl << endl;
                cout << "Name: " << OrganisationRecords[count].name << endl;
                cout << "Adress: " <<  HRRecords[count].address << endl;
                cout << "Department: " << OrganisationRecords[count].department << endl;
                cout << "Salary: " << PayrollRecords[count].salary << endl;
                cout << "=============================================" << endl;
                cout << OrganisationRecords.size();
            }
        }

        return;
    }

how can i implicitly make the employee number available for display in " displayEmployeeOfSalaryGTE ". 我如何隐式地使员工编号可在“ displayEmployeeOfSalaryGTE ”中显示。

According to your reading code, every organization record has an employee number. 根据您的阅读代码, 每个组织记录都有一个员工编号。 So simply add a field for that value to your OrganisationRecord class, then you will have access to it in displayEmployeeOfSalaryGTE() , eg: 因此,只需在OrganisationRecord类中为该值添加一个字段,然后您就可以在displayEmployeeOfSalaryGTE()访问它,例如:

class OrganisationRecord
{
public:
    int employeeNumber; // <-- add this
    ...
};

...

void PayrollProcessing::loadOrganisationRecords(string filename)
{
    ...

    int employeeNumber;
    OrganisationRecord _organisationrecord;

    while (inputfile >> employeeNumber)
    {
        inputfile.ignore();
        _organisationrecord.employeeNumber = employeeNumber // <-- add this
        ...
    }

    ...
}

...

cout << "EmployeeNumber: " << OrganisationRecords[count].employeeNumber << endl;

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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