首页 > 解决方案 > 在类中创建一个向量,然后在函数中使用类对象不起作用

问题描述

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

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

这是插入的函数,但打印向量的元素根本不起作用:

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

现在是删除函数,如果我输入一个字符串,然后在向量中搜索它,然后获取他的索引然后删除,但向量不会更新任何值。

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

这是主要功能,向量的值也不打印:

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

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

标签: c++oopvector

解决方案


这段代码有很多问题。但是您要问的特定问题是由您的函数按值Employees传递对象引起,因此制作了一个副本,并且您对副本所做的任何更改都不会反映在.main()

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

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

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

我建议从头开始完全重写代码,例如更像这样:

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

推荐阅读