私は映画レンタル事業(理論的には)のバイナリ検索ツリーを作っています。ムービーレンタル事業にないタイトルが入力された場合は、「ムービーが見つかりません」と出力する必要があります。毎回セグメンテーションフォルトを取得せずにこれを行う方法がわかりません。私はこのコードが理にかなっていると私に何を試すべきか分からない。私は初心者です。私は簡単に行ってください。どんな助けでも大歓迎です! (あなたは私のコードの終わりに向かって3つのスラッシュ(///)の間で「ムービーが見つかりません」を出力しようとした私の場合は機能を見つけることができます。バイナリ検索ツリーのセグメント化エラー
void MovieTree::rentMovie(std::string title)
{
MovieNode *foundMovie = root;
//MovieNode *parent = NULL;
if (root == NULL)
{
cout << "Movie not found." << endl;
return;
}
else
{
foundMovie = root;
while (foundMovie != NULL)
{
if (foundMovie == NULL) //tree is empty
{
cout << "Movie not found." << endl;
}
else
{
if (foundMovie->title.compare(title) > 0) //uses ASCII to determine where titles are
{
foundMovie->parent = foundMovie;
foundMovie = foundMovie ->leftChild;
//cout << "printed left" << endl; //debugging
if (foundMovie->title.compare(title) == 0 && foundMovie->quantity > 0)
{
foundMovie->quantity--;
cout << "Movie has been rented." << endl;
//Title entered matches title found
cout << "Movie Info:" << endl;
cout << "===========" << endl;
cout << "Ranking:" << foundMovie->ranking << endl;
cout << "Title:" << foundMovie->title << endl;
cout << "Year:" << foundMovie->year << endl;
cout << "Quantity:" << foundMovie->quantity << endl;
break;
}
else if (foundMovie->quantity ==0)
{
//If movie is out of stock
cout << "Movie out of stock." << endl;
break;
}
}
else //check rightChild
{
foundMovie->parent = foundMovie;
foundMovie = foundMovie->rightChild;
//cout << "printed right" << endl; //debugging
if (foundMovie->title.compare(title) == 0 && foundMovie->quantity > 0) //title entered matches title found
{
foundMovie->quantity--;
cout << "Movie has been rented." << endl;
cout << "Movie Info:" << endl;
cout << "===========" << endl;
cout << "Ranking:" << foundMovie->ranking << endl;
cout << "Title:" << foundMovie->title << endl;
cout << "Year:" << foundMovie->year << endl;
cout << "Quantity:" << foundMovie->quantity << endl;
break;
}
else if (foundMovie->quantity ==0)
{
//movie is found but out of stock
cout << "Movie out of stock." << endl;
break;
}
}
}
}
///
if (foundMovie->title == title)
{
cout << "found the movie" << endl;
}
else
{
cout << "Movie not found." << endl;
}
///
}
}
https://ericlippert.com/2014/03/05/how-to-debug-small-programs/ – slim
'foundMovie->親= foundMovie; 'は良い考えではありません。循環依存を作成します。 – Franck
もう1つの推奨事項:ツリーロジックからムービーロジックを切り離します。これにより、ツリーとムービーを別々にテストし、障害のあるものを簡単に判別することができます。両方のテストを同時に行うと、実際にはバグを修正し、他のバグのためにそれを認識できない厄介な場所に置かれます。 – user4581301