无法从集合中删除shared_ptr

Reu*_*tta 1 c++ set shared-ptr

我试图拥有一个带有一组指向另一个对象的指针的对象。当我尝试删除集合的值时,我收到错误并崩溃,我真的不知道是什么原因导致的。这是库,之后是主要功能:当我尝试运行它时,它会执行它应该执行的所有操作,当它到达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;
}
Run Code Online (Sandbox Code Playgroud)

这是主要功能:

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;
}
Run Code Online (Sandbox Code Playgroud)

Sam*_*hik 5

shared_ptr<Employee> employee(employee_add);
Run Code Online (Sandbox Code Playgroud)

shared_ptr首先,只有一个理由:它存在的理由只有一个;正如每一本 C++ 教科书所解释的那样,它的生命周期中只有一个使命:能够访问new一个对象,并在对该对象的所有引用都消失时shared_ptr自动处理该对象,从而避免内存泄漏。delete

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

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

    // etc, etc, etc...
Run Code Online (Sandbox Code Playgroud)

这就是坠机的原因。

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