2013-04-29 93 views
0

我的代码有问题。我已经运行并调试了好几次。它似乎工作正常,如果我不在我的getEntry函数中引发异常。但是当我抛出异常时,我的程序在此之后出现了分段错误。当我通过程序进行调试时,似乎getEntryHelper中的nextNodePtr不是0x0。所以在抛出异常之后不知何故被更改,我不知道为什么。未知Segementation错误

我的主:

#include <iostream> 
#include "BinarySearchTree.h" 

int main { 
BinarySearchTree<std::string,std::string> myTree; 
    myTree.add("book"); 
     myTree.add("encyclopedia"); 
    myTree.add("automobile"); 
    myTree.add("zebra"); 
     myTree.getEntry(zebra); 
     myTree.getEntry(xylophone); 
     myTree.getEntry(tree); // Does not get to here 

} 

这里是我加的,并getEntry方法(虽然好像我getEntry的问题是:

template<typename KeyType, typename ItemType> 
void BinarySearchTree<KeyType,ItemType>::add(const ItemType& newEntry) { 
    if(rootPtr == NULL) 
     rootPtr = new BinaryNode<ItemType>(newEntry); 
    else { 
     addHelper(rootPtr,rootPtr,newEntry); 
    } 
} 

template<typename KeyType, typename ItemType> 
ItemType BinarySearchTree<KeyType,ItemType>::getEntry(const KeyType& aKey) const 
     throw(NotFoundException) { 
    try { 
     BinaryNode<ItemType>* temp = getEntryHelper(rootPtr,aKey); 
     std::cout << temp->getItem() << "\n"; 
     return temp->getItem(); 
    } 
    catch(NotFoundException& nf) { 
     std::cout << nf.what(); 
    } 
} 

template<typename KeyType, typename ItemType> 
void BinarySearchTree<KeyType,ItemType>::addHelper(BinaryNode<ItemType>* prevNodePtr, 
                BinaryNode<ItemType>* nextNodePtr, 
                const ItemType& newEntry) { 

    if(nextNodePtr == NULL) { // Base Case 
     nextNodePtr = new BinaryNode<ItemType>(newEntry,NULL,NULL); 
     if(newEntry < prevNodePtr->getItem()) 
      prevNodePtr->setLeftChildPtr(nextNodePtr); 
     else 
      prevNodePtr->setRightChildPtr(nextNodePtr); 
     return; 
    } 
    if(newEntry < nextNodePtr->getItem()) { 
     prevNodePtr = nextNodePtr; 
     nextNodePtr = nextNodePtr->getLeftChildPtr(); 
     addHelper(prevNodePtr,nextNodePtr,newEntry); 
    } 
    else { 
     prevNodePtr = nextNodePtr; 
     nextNodePtr = nextNodePtr->getRightChildPtr(); 
     addHelper(prevNodePtr,nextNodePtr,newEntry); 
    } 
} 

template<typename KeyType, typename ItemType> 
BinaryNode<ItemType>* BinarySearchTree<KeyType,ItemType>::getEntryHelper(BinaryNode<ItemType>* nextNodePtr,const KeyType& aKey) const { 
    if(nextNodePtr == NULL) { 
     throw NotFoundException("does not exist in tree.\n"); 
    } 
    else if(nextNodePtr->getItem() == aKey) 
     return nextNodePtr; 
    else if(aKey < nextNodePtr->getItem()) { 
     getEntryHelper(nextNodePtr->getLeftChildPtr(),aKey); 
    } 
    else { 
     getEntryHelper(nextNodePtr->getRightChildPtr(),aKey); 
    } 
} 

输出: 汽车 书 百科全书 斑马 zebra 先决条件违反例外:树木中不存在木琴 分段错误(核心转储)

+0

我想你需要在日志记录之后重新引入getEntry - 否则返回的值是未定义的,不是? – 2013-04-29 16:58:32

+0

我没有意识到可以改变指针值。它现在有用,谢谢! – user2041391 2013-04-29 18:13:43

+0

如果从getEntryHelper()函数抛出NotFoundException,那么你不应该有任何问题,因为它被getEntry()函数捕获。我觉得它是一个悬而未决的指针问题,nextnodePtr没有被分配,并且触发了在任何导致分段错误的地方都没有捕获到的异常。我猜你没有在BinaryNode (newEntry)的构造函数中将左和右ptr的值设置为null。 – 2013-04-29 18:52:07

回答

1

快速查看一下,我发现很少有一些问题的例外处理不当。

  1. 功能getEntry()信号,它可以thow NotFoundException,但在main()我看不到它的任何异常处理程序。因此,在main()函数中放置一个基本的try catch可以处理任何异常。

    int main() 
    { 
    try 
    { 
        //some code 
    } 
    catch(..) 
    { 
        cout << "Unkown Exception"; 
    } 
    return 0; 
    } 
    
  2. 功能getEntry()信号,它可以thow NotFoundException,但你必须在那里你处理异常,但从来没有再扔也不是异常触发任何新的\修改NotFoundException一个try catch块。如果你不想抛出它,那么在函数声明中注释throw(NotFoundException)。

    ItemType BinarySearchTree<KeyType,ItemType>::getEntry(const KeyType& aKey) //const throw (NotFoundException) -> Comment this 
    

如果不处理NotFoundException后,重新抛出。

catch(NotFoundException& nf) { 
    std::cout << nf.what(); 
    rethrow; 
} 

但是还是我不知道,如果你的二进制代码插入逻辑工作正常。发布整个头文件和cpp文件,如果遇到任何逻辑问题。