我已经实现了一个名为 LipidType 的类,但是当调用调整大小函数和赋值运算符重载时程序崩溃,但我无法在代码中找出错误。这些是类的私有成员
private:
std::string *mod;
std::string *classe;
int matches;
double mz;
double *intensity;
double *frag;
std::string name;
int maxsize;
int length;
这些是功能
LipidType& LipidType::operator=(const LipidType& otherlip) //assignment operator
{
if (this == &otherlip) return *this; // handle self assignment
if(otherlip.maxsize > maxsize)
{
std::cout << "Resizing" << std::endl;
resize(otherlip.maxsize);
}
for(int i=0; i<otherlip.length; i++)
{
mod[i]=otherlip.mod[i];
classe[i]=otherlip.classe[i];
intensity[i]=otherlip.intensity[i];
frag[i]=otherlip.frag[i];
}
matches=otherlip.matches;
name=otherlip.name;
mz=otherlip.mz;
length = otherlip.length;
return *this;
}
void LipidType::resize(int nusize)
{
maxsize = nusize;
std::string* temp1=new std::string[maxsize];
std::string* temp2=new std::string[maxsize];
double* temp3=new double[maxsize];
double* temp4=new double[maxsize];
for(int i=0; i<length; i++)
{
temp1[i]=mod[i];
temp2[i]=classe[i];
temp3[i]=intensity[i];
temp4[i]=frag[i];
}
delete [] mod;
delete [] classe;
delete [] intensity;
delete [] frag;
mod=temp1;
classe=temp2;
intensity=temp3;
frag=temp4;
}