0
尝试将字符串添加到动态数组时,出现“EXC_BAD_ACCESS”错误。难道我做错了什么?下面是一些代码片段:无法将字符串分配给动态数组位置
typedef unsigned short ushort_t;
typedef string* stringPtr_t;
class Doctor {
private:
string doctorName;
stringPtr_t patientArray;
ushort_t patientArraySize;
ushort_t numOfPatient;
bool Doctor::addPatient(string patientName)
{
patientArray[numOfPatient].assign(patientName);
numOfPatient++;
return true;
}
Doctor& Doctor::operator =(const Doctor& docSource)
{
for (int i = 0; i < docSource.patientArraySize; i++) {
patientArray[i].assign(docSource.patientArray[i]);
}
return *this;
}
};
int main()
{
Doctor testDoc5(2);
cout.clear();
assert(testDoc5.addPatient("Bob Smith")==true);
}
Doctor::Doctor(ushort_t patientArrayCapacity)
: doctorName("need a name.")
, patientArraySize(patientArrayCapacity)
, numOfPatient(0)
{
patientArray = *new stringPtr_t[patientArraySize];
}
patientArray分配给了哪里? – Mark
您是否为patientArray分配了任何内存? – veda
@Mark将其分配给先前构造的医生对象,我将编辑以显示此内容。 – user3373291