Creating a vector in class then using class object in function not working

独自空忆成欢 提交于 2021-02-10 12:53:27

问题


I have a class Employees. I'm trying to make the user insert and delete an employee but it's not working. The size of the vectors should be 500.

class Employees{
public:
    int maxx = 500;
    vector<string> Surname;
    vector<string> FirstName;
    vector<string> birthdate;
    int vacation[500];
public:
    Employees() : Surname(500) {}
};

This is the function that inserts, but printing elements of the vectors is not working at all:

void Process(Employees ZZ){

    string dateyear;
    string datemonth;
    string dateday;
    int dateyear1;
    int datemonth1;
    int dateday1;
    int Realage;
    int Vacationi = 0;

    for(int i = 0; i < 500; i++) {

        string s;
        cin >> s;
        string d;
        cin >> d;
        string c;
        cin >> c;

        ZZ.Surname.push_back(s);
        ZZ.FirstName.push_back(d);
        ZZ.birthdate.push_back(c);

        cout << endl << ZZ.Surname[1] << endl;
    }

Now the delete function, if I input a string then search for it in the vector then get his index then delete, but the vector doesn't update any values.

void DeleteEmployee(Employees ZZ){

    cout<< endl <<  ZZ.Surname[1] << endl ;

    for (int i = 0; i < ZZ.Surname.size(); i++){
        cout << ZZ.Surname[i] ;
    }
    cout << " delete employee";
    string delete1;
    cin >> delete1;

    auto it = std::find(ZZ.Surname.begin(), ZZ.Surname.end(), delete1);
    if (it == ZZ.Surname.end())
    {
        cout<< " name not in vector "  << endl;
    }
    else
    {
        //auto index = distance(Names.begin(), find(Names.begin(), Names.end(), old_name_)));
        //ZZ.Surname.erase(ZZ.Surname.begin()+index) ;
    }
}

This is the main function, also the values of the vector are not printing:

int main()
{
    Employees ZZ;
    Process(ZZ);
    DeleteEmployee(ZZ);
    cout << "fyccck";

    for (int i = 0; i < ZZ.Surname.size(); i++){
        cout << ZZ.Surname[i] ;
    }
}

回答1:


There are a lot of things wrong with this code. But the particular issue you are asking about is caused by your functions passing the Employees object by value, so a copy is made, and any changes you make to the copy are not reflected in the original object in main().

You need to change the parameters to pass the Employees object by reference instead:

void Process(Employees &ZZ)
void DeleteEmployee(Employees &ZZ)

That being said, the whole design of the code is not good in general. The vectors are not being kept in sync properly, and for that matter you are using more vectors then you actually need, 1 single vector will suffice. And Process() and DeleteEmployee() should be members of the Employees class, not separate functions. And they are both accessing out-of-bounds of the Surname vector.

I would suggest completely rewriting the code from scratch, for instance something more like this:

struct Employee{
    string Surname;
    string FirstName;
    string BirthDate;
    int Vacation;

    string DisplayName() const { return Surname + ", " + FirstName; }
};

class Employees{
public:
    static const int maxx = 500;
    vector<Employee> employees;

    Employees() { employees.reserve(maxx); }

    bool Add(const Employee &e);
    bool Delete(string Surname, string FirstName);
};

bool Employees::Add(const Employee &e) {
    if (employees.size() < maxx) {
        employees.push_back(e);
        return true;
    }
    return false;
}

bool Employees::Delete(string Surname, string FirstName) {
    auto it = std::find_if(employees.begin(), employees.end(),
        [&](const Employee &e){
            return e.Surname == Surname && e.FirstName == FirstName;
        }
    );
    if (it != employees.end()) {
        employees.erase(it);
        return true;
    }
    return false;
}

int main()
{
    Employees ZZ;

    for(int i = 0; i < Employees::maxx; ++i) {
        Employee e;
        cin >> e.Surname;
        cin >> e.FirstName;
        cin >> e.BirthDate;
        e.Vacation = 0;//cin >> e.Vacation;

        ZZ.Add(e);

        cout << endl << e.DisplayName() << endl;
    }

    cout << " delete employee";
    string Surname, FirstName;
    if (cin >> Surname >> FirstName) {
        if (ZZ.Delete(Surname, FirstName)) {
            cout << " name deleted from vector " << endl;
        } else {
            cout << " name not in vector " << endl;
        }
    }

    cout << "fyccck";

    for (auto &e : ZZ.employees) {
        cout << e.DisplayName() << endl;
    }

    return 0;
}


来源:https://stackoverflow.com/questions/65117849/creating-a-vector-in-class-then-using-class-object-in-function-not-working

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!