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