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;
}
1つの要素を配列から削除したいだけですが、その要素を削除するとクラッシュすることがあります。 [i]は動的に割り当てられた配列を削除するC++
学生はポインタ配列である、と私はあなたがstudentnameを見つけることができれば、学生の各インスタンスを削除したいと思われ、選択した要素
[配列要素を削除して残りの要素を移動する]の可能な複製(http://stackoverflow.com/questions/879603/remove-an-array-element-and-shift-the-remaining-ones) – MSD
参照ソリューションは、[この(http://stackoverflow.com/questions/9246165/how-to-remove-elements-from-dynamically-allocated-array)古いSOの質問で提供されます。 –
'StudentList :: students'の定義を参照する必要があります(配列、ポインタ、unique_ptrsのベクトル、または何ですか?)、および基本型の参照)。また、「動作停止」とは何かを知る必要があります。コードがクラッシュすると、私たちが[mcve]を持っていなければ、これはおそらく閉じられるでしょう。 –