bool StudentList::remove(const char * studentName)
{
for (int i = 0; i < MAX_SIZE; i++)
{
if (this->students[i]->isEqualTo(studentName)) // Finds a name to remove
{
cout << "Remove: "; // Displays name wished to be removed
students[i]->print();
// delete[] students[i]; - Crashes
// students[i] = NULL; - Replaces removed name with null, stops working.
// students[i]->~Student(); - Call deconstructor, Crashes.
return true;
}
}
return false;
}
我只想從數組中刪除單個元素,但當我刪除該元素時不斷崩潰。刪除動態分配的數組C++
學生[i]是一個指針數組,我需要刪除選定的元素
可能重複的[刪除數組元素並移動其餘的](http://stackoverflow.com/questions/879603/remove-an-array-element-and-shift-the-remaining-ones) – MSD
請參閱在[this]中提供的解決方案(http://stackoverflow.com/questions/9246165/how-to-remove-elements-from-dynamically-allocated-array)舊的SO問題。 –
我們需要看到'StudentList :: students'(它是一個數組,一個指針,一個unique_ptrs矢量,還是什麼?)以及基類型的定義。我們還需要知道「停止工作」是什麼意思,並且*代碼崩潰時。如果我們沒有[mcve],這可能會關閉。 –