无法从集合中删除 shared_ptr

Cannot erase a shared_ptr from set

我正在尝试让一个对象具有一组指向另一个对象的指针。当我尝试擦除该集合的值时,出现错误并崩溃,我真的不知道是什么原因造成的。这是库,然后是主要功能: 当我尝试 运行 它会做它应该做的一切,当它到达 removeemployee 时它崩溃并发出以下消息:进程完成,退出代码 -1073740940 (0xC0000374)

我 运行 如果重要的话,它在 clion 上,在 c++11 中。

#include <ostream>
#include <iostream>
#include "Manager.h"

Manager::Manager(int id, string firstName, string lastName, int birthYear)
        : Citizen(id, firstName, lastName,birthYear), salary(0), employees(), work_flag(false) {}

int Manager::getSalary() const {
    return salary;
}

void Manager::setSalary(int _salary) {
    if((salary + _salary) < 0){
        salary = 0;
    }else {
        salary += _salary;
    }
}

void Manager::addEmployee(Employee* employee_add) {
    shared_ptr<Employee> employee(employee_add);
    if(employees.find(employee) != employees.end()){
        throw mtm::EmployeeAlreadyExists();
    }
    employees.emplace(employee);
}

//this is the function

void Manager::removeEmployee(int id) {
    for(auto it = employees.begin(); it != employees.end(); it++){
        if(it->get()->getId() == id){
            employees.erase(it);
            return;
        }
    }
    throw mtm::EmployeeDoesNotExists();
}

Manager *Manager::clone() {
    return new Manager(*this);
}

ostream &Manager::printShort(ostream &os) const {
    os<<this->getFirstName()<<" "<<this->getLastName()<<endl;
    os<<"Salary :"<<this->getSalary()<<endl;
    return os;
}

ostream &Manager::printLong(ostream &os) const {
    os<<this->getFirstName()<<" "<<this->getLastName()<<endl;
    os<<"id - "<<this->getId()<<" birth_year - "<<this->getBirthYear()<<endl;
    os<<"Salary :"<<this->getSalary()<<endl;
    os<<"Employees:"<<endl;
    for(const auto & employee : employees){
        employee->printShort(os);
    }
    return os;
}

bool Manager::findEmployee(int id) {
    int i = 0;
    for(const auto & employee : employees){
        cout<<++i<<endl;
        if(employee->getId() == id){
            cout<<"return true"<<endl;
            return true;
        }
    }
    cout<<"return false"<<endl;
    return false;
}

bool Manager::isWorkFlag() const {
    return work_flag;
}

void Manager::setWorkFlag(bool workFlag) {
    work_flag = workFlag;
}

这是主要功能:

int main() {
    Employee e1(1, "John", "Williams", 2002);
    Employee e2(2, "Alex", "Martinez", 2000);
    Manager m1(1,"Robert", "stark", 1980);
    m1.addEmployee(&e1);
    m1.addEmployee(&e2);
    Employee e3(7, "Reuven", "Guetta", 2001);
    m1.addEmployee(&e3);
    m1.printLong(cout);
    cout<<"Delete"<<endl;
    //here is the problem
    m1.removeEmployee(e2.getId());
    m1.printLong(cout);
    return 0;
}
shared_ptr<Employee> employee(employee_add);

首先,拥有 shared_ptr 的原因只有一个;它的存在只有一个原因;正如每一本 C++ 教科书所解释的那样,它一生只有一个使命:能够 new 一个对象,并让 shared_ptr 在所有引用时自动处理 deleteing 它对象消失了,避免了内存泄漏。

在您的程序中,此对象未在 new 的动态范围内实例化:

    Employee e2(2, "Alex", "Martinez", 2000);
    Manager m1(1,"Robert", "stark", 1980);
    m1.addEmployee(&e1);

    // etc, etc, etc...

这就是崩溃的原因。

如果您不使用 new,只需删除所示代码中的所有 shared_ptr