私はgdbを初めて使用し、一般的にデバッグを行っていますが、gdbなしでこれまでどのようにこれを行ってきたのかわかりません。gdb が教えてくれたプログラムのデバッグは、この関数にセグメンテーション違反があることを示しています。以下のコメントで強調します。
template <class elemType>
struct nodeType
{
elemType info;
nodeType<elemType> *lLink;
nodeType<elemType> *rLink;
};
#define H_binarySearchTree
#include <iostream>
#include "binaryTree.h"
using namespace std;
template <class elemType>
class bSearchTreeType: public binaryTreeType<elemType>
{
public:
bool search(const elemType& searchItem) const;
//Function to determine if searchItem is in the binary
//search tree.
//Postcondition: Returns true if searchItem is found in
// the binary search tree; otherwise,
// returns false.
void insert(const elemType& insertItem);
//Function to insert insertItem in the binary search tree.
//Postcondition: If there is no node in the binary search
// tree that has the same info as
// insertItem, a node with the info
// insertItem is created and inserted in the
// binary search tree.
void deleteNode(const elemType& deleteItem);
//Function to delete deleteItem from the binary search tree
//Postcondition: If a node with the same info as deleteItem
// is found, it is deleted from the binary
// search tree.
// If the binary tree is empty or deleteItem
// is not in the binary tree, an appropriate
// message is printed.
void printTree();
void printTree(nodeType<elemType> *p);
// void printTreeNode(nodeType<elemType> *p);
void swapSubtrees(nodeType<elemType> *root1);
void swapSubtrees();
nodeType<elemType> *root1;
template <class elemType>
void bSearchTreeType<elemType>::insert
(const elemType& insertItem)
{
nodeType<elemType> *current; //pointer to traverse the tree
nodeType<elemType> *trailCurrent; //pointer behind current
nodeType<elemType> *newNode; //pointer to create the node
newNode = new nodeType<elemType>;
newNode->info = insertItem;
newNode->lLink = NULL;
newNode->rLink = NULL;
if (root1 == NULL)
root1 = newNode;
else
{
current = root1;
while (current != NULL)
{
trailCurrent = current;
if (current->info == insertItem)//** This is where gdb says there is a seg fault
{
cout << "The item to be inserted is already ";
cout << "in the tree -- duplicates are not "
<< "allowed." << endl;
return;
}
else if (current->info > insertItem)
current = current->lLink;
else
current = current->rLink;
}//end while
if (trailCurrent->info > insertItem)
trailCurrent->lLink = newNode;
else
trailCurrent->rLink = newNode;
}
}//end insert
template<class elemType>
void bSearchTreeType<elemType>::swapSubtrees(nodeType<elemType> * root1)
{
if (root1 != NULL)
{
nodeType<elemType> *temp;
swapSubtrees(root1->lLink);//Seg Fault here as well
swapSubtrees(root1->rLink);
temp =root1->lLink;
root1->lLink = root1->rLink;
root1->rLink = temp;
}
}
}
誰かが何が起こっているのか、それを修正するために何をする必要があるのか を説明してもらえますか?
*より多くの詳細を含めるために編集されました