2012-11-06 111 views
0

我是gdb和一般調試的新手,我不確定我是如何在沒有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; 
     } 
    } 
} 

可能有人請幫助描述是怎麼回事,什麼我需要做什麼來解決這個問題:我將在下面留言突出呢?

*編輯,以包括更多細節 大加讚賞

+1

root1從哪裏來?編輯 – imreal

+0

以包含更多詳細信息 – Craig

+0

如何聲明'nodeType :: info'? –

回答

1

你忘了root1NULL在構造函數 - 事實上,你似乎沒有有一個。