在类中创建一个向量然后在函数中使用类对象不起作用 [英] Creating a vector in class then using class object in function not working

查看:49
本文介绍了在类中创建一个向量然后在函数中使用类对象不起作用的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个班级Employees.我试图让用户插入和删除员工,但它不起作用.向量的大小应为 500.

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] ;
    }
}

推荐答案

这段代码有很多问题.但是您要问的特定问题是由您的函数通过Employees 对象按值 引起的,因此进行了复制,以及任何更改你对 copy 的复制不会反映在 main() 中的 original 对象中.

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().

您需要更改参数以通过引用传递Employees对象:

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

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


话虽如此,整个代码的设计总体上并不好.向量没有正确保持同步,就此而言,您使用的向量比实际需要的向量多,1 个 vector 就足够了.而 Process()DeleteEmployee() 应该是 Employees 类的成员,而不是单独的函数.并且它们都访问了 Surname 向量的边界.


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

这篇关于在类中创建一个向量然后在函数中使用类对象不起作用的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆