我已经实现了以下程序来理解复合设计模式。我也使用了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;
}
答案 0 :(得分:3)
更改
else
myTeam.erase(myTeam.begin()+pos);
到
else {
myTeam.erase(myTeam.begin()+pos);
break;
}
修复崩溃(demo on ideone;没有break
,it crashes with SIGSEGV)。问题是,即使在擦除其中一个元素之后,您仍继续迭代向量,这是不允许的。
由于您只删除一名工作人员(假设ID
是唯一的),因此在元素被删除后继续进行并不是一个好主意。
答案 1 :(得分:3)
你的问题是C ++和STL的一个非常常见的陷阱:你在迭代过程中没有经过适当的关注就会被删除。这里:
for (auto temp : myTeam)
{
if (temp->getID()!=employee->getID())
++pos;
else
myTeam.erase(myTeam.begin()+pos);
}
一般来说,在迭代它时从STL容器中擦除是不行的。相反,可以考虑“标记”稍后要删除的元素,或者使用基于整数或迭代器的迭代,以便您可以更精确地控制它。我可能会在这里使用一个简单的基于整数的for循环,因为无论如何你都有这个“pos”变量。
答案 2 :(得分:0)
问题可能出在您的deleteWorker方法中。您正在修改它时迭代容器。
我建议使用两种可能的解决方案之一