c++ - 无法理解为什么会出现段错误?

标签 c++ design-patterns c++11 segmentation-fault

我已经实现了以下程序来理解复合设计模式。我也使用了 C++11 中的几个概念。但对我不利的是,该程序在运行时出现段错误。我尝试使用 GDB 进行调试,发现 getID() 函数存在一些问题。

#0  0x08049a12 in Employee::getID (this=0x0) at Composite.cpp:27
27      int getID(){return ID;}

但我仍然无法理解该功能有什么问题?感谢有人可以提供帮助。

#include <iostream>
#include <memory>
#include <string>
#include <vector>
#include <algorithm>

using namespace std;

class Employee
{
    protected:
    int ID;
    string Name;
    string Role;

    public:
    Employee(int empID, string empName, string empRole)
    {
        ID=empID;
        Name=empName;
        Role=empRole;
    }
    virtual void showDetails()=0;
    virtual void addWorker(shared_ptr<Employee> newWorker)=0;
    virtual void deleteWorker(shared_ptr<Employee> employee)=0;
    virtual ~Employee(){}
    int getID(){return ID;}
};

class Worker : public Employee
{
    public:
    Worker(int empID, string empName, string empRole)
        : Employee(empID, empName, empRole) {}

    void showDetails()
    {
        cout<<Name<<" ("<<ID<<") "<<Role<<endl;
    }

    void addWorker(shared_ptr<Employee> newWorker){};
    void deleteWorker(shared_ptr<Employee> employee){};
};

class Supervisor : public Employee
{
    private:
    vector<shared_ptr<Employee>> myTeam;

    public:
    Supervisor(int empID, string empName, string empRole)
        : Employee(empID, empName, empRole) {}

    void addWorker(shared_ptr<Employee> newWorker)
    {
        myTeam.push_back(newWorker);
    }

    void deleteWorker(shared_ptr<Employee> employee)
    {
        int pos=0;
        for (auto temp : myTeam)
        {
        if (temp->getID()!=employee->getID())
            ++pos;
        else
            myTeam.erase(myTeam.begin()+pos);
        }
    }

    void showDetails()
    {
        cout<<Name<<" ("<<ID<<") "<<Role<<" ---->"<<endl;
        for (auto worker : myTeam)
        {
        worker->showDetails();
        }
        cout<<endl;
    }
};

int main()
{
    shared_ptr<Employee> Tushar(new Worker(376653,"Tushar Shah","Team mate"));
    shared_ptr<Employee> Ranjeet(new Worker(469725,"Ranjeet Aglawe","Team mate"));
    shared_ptr<Employee> Kiran(new Supervisor(137581,"Kiran Asher","Manager"));
    shared_ptr<Employee> Namita(new Supervisor(122110,"Namita Gawde","Manager"));
    shared_ptr<Employee> Rumman(new Supervisor(122022,"Rumman Sayed","Manager"));
    shared_ptr<Employee> Rajendra(new Supervisor(111109,"Rajendra Redkar","Manager"));
    shared_ptr<Employee> Sameer(new Supervisor(106213,"Sameer Rajadhyax","Group Lead"));

    Kiran->addWorker(Tushar);
    Kiran->addWorker(Ranjeet);
    Sameer->addWorker(Kiran);
    Sameer->addWorker(Namita);
    Sameer->addWorker(Rumman);
    Sameer->addWorker(Rajendra);

    Sameer->showDetails();

    Sameer->deleteWorker(Rumman);
    Sameer->showDetails();

    return 0;
}

最佳答案

改变

else
    myTeam.erase(myTeam.begin()+pos);

else {
    myTeam.erase(myTeam.begin()+pos);
    break;
}

修复崩溃( demo on ideone ;没有 break, it crashes with SIGSEGV )。问题是即使在删除其中一个元素后,您仍继续迭代 vector ,这是不允许的。

因为您只打算删除一个工作人员(假设 ID 是唯一的),所以在删除该元素之后继续操作无论如何都不是一个好主意。

关于c++ - 无法理解为什么会出现段错误?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/14797517/

相关文章:

C++程序控制终端窗口

java - 这是对单例的适当使用吗?

c++ - 使用指针 vector 创建线程

c++ - Qt客户端——连接C服务器

c++ - 我真的需要为const对象实现用户提供的构造函数吗?

C++:这个模式有名字吗,可以改进吗?

java - 在哪里测试父类(super class)的默认方法实现

c++ - 将 vector 分配给多重集

c++ - 为什么我不能将 C 样式的数组复制到 std::array?

c++ - 命名困惑?名为 FlowerGroup 和 FlowerGroups 的对象是否令人困惑?