我有以下类,当调用析构函数并尝试删除指向 a 和 b 的指针时出现错误。看起来它们不存在。这是触发问题的代码行:
unordered_map<string,Stock>* SDict = new unordered_map<string,S>();
SDict->insert(make_pair("38363",S("38363",i,w)));
标题
class O{
public:
O();
~O();
O(const O& tocopy);
O& operator=(const O& toassign);
private:
unordered_map<int,PQL>* b;
unordered_map<int,PQL>* a;
};
资源
O::O(){
a = new unordered_map<int,PQL>();
b = new unordered_map<int,PQL>();
}
O::~O(){
delete b; //I get the exception here- b doesn't exist before the delete.
delete a;
}
O& O::operator=(const O& src){
if(this != &src){
delete b;
delete a;
b = new unordered_map<int,PQL>();
b = src.b;
a = new unordered_map<int,PQL>();
a = src.a;
}
return *this;
}
O::O(const O& src){
b = new unordered_map<int,PQL>();
b = src.b;
a = new unordered_map<int,PQL>();
a = src.a;
}
PQL 是一个只有三个整数的类。是否有明显的原因导致此错误?
类 O 是以下类的数据成员:
标题
class S{
public:
S();
S(string sid, vector<string>* indexids, vector<double>* sw);
~S();
S(const S& tocopy);
S& operator=(const S& toassign);
private:
string sid;
O* o;
vector<Strategy*> ts;
unordered_map<string,double>* iw;
};
资源
S::S(){}
S::S(string sid, vector<string>* iis, vector<double>* sw){
sid = sid;
iw = new unordered_map<string,double>();
o = new o();
if(iis->size() == sw->size()){
for(size_t i=0; i<iis->size(); i++){
string key = iis->at(i);
if(iw->count(key) == 0 ){
double weighting = sw->at(i);
iw->insert(make_pair(key,weighting));
}
else{
throw new exception();
}
}
}
else{
throw new exception();
}
}
S::S(const S& rhs){
sid = rhs.sid;
ts = rhs.ts;
o = new O();
o = rhs.o;
iw = new unordered_map<string,double>();
iw = rhs.iw;
}
S& S::operator=(const S& src){
if(this != &src){
delete o;
delete iw;
sid = src.sid;
ts = src.ts;
o = new o();
o = src.o;
iw = new unordered_map<string,double>();
iw = src.iw;
}
return *this;
}
S::~S(){
delete o;
delete iw;
}