2008-12-13 62 views
1

hI,我試圖從Larry Nyhoff的書中得到這段代碼在Bloodshed中編譯。它實際上是從作者的網站一字不提,儘管我聲明它在.cpp而不是.h(.h文件不與測試應用程序一起工作)。讓這個BST模板起作用

http://cs.calvin.edu/activities/books/c++/ds/2e/SourcePrograms/Chap12/

搜索(常量數據類型&項目)的功能是什麼給我的悲傷。編譯器錯誤說:

In member function `bool BST<DataType>::search(const DataType&) const': 
expected `;' before "locptr" 
`locptr' undeclared (first use this function) 

我在這裏錯過了什麼?

#include <iostream> 

#ifndef BINARY_SEARCH_TREE 
#define BINARY_SEARCH_TREE 


template <typename DataType> 
class BST 
{ 
public: 
    /***** Function Members *****/ 
    BST(); 

    bool empty() const; 

    bool search(const DataType & item) const; 

    void insert(const DataType & item); 

    void remove(const DataType & item); 

    void inorder(std::ostream & out) const; 

    void graph(std::ostream & out) const; 

    private: 
    /***** Node class *****/ 
    class BinNode 
    { 
    public: 
    DataType data; 
    BinNode * left; 
    BinNode * right; 

    // BinNode constructors 
    // Default -- data part is default DataType value; both links are null. 
    BinNode() 
    : left(0), right(0) 
    {} 

    // Explicit Value -- data part contains item; both links are null. 
    BinNode(DataType item) 
    : data(item), left(0), right(0) 
    {} 


}; //end inner class 

typedef BinNode * BinNodePointer; 

    /***** Private Function Members *****/ 
    void search2(const DataType & item, bool & found, 
       BinNodePointer & locptr, BinNodePointer & parent) const; 
/*------------------------------------------------------------------------ 
    Locate a node containing item and its parent. 

    Precondition: None. 
    Postcondition: locptr points to node containing item or is null if 
     not found, and parent points to its parent.#include <iostream> 
------------------------------------------------------------------------*/ 

    void inorderAux(std::ostream & out, 
        BST<DataType>::BinNodePointer subtreePtr) const; 
    /*------------------------------------------------------------------------ 
    Inorder traversal auxiliary function. 

    Precondition: ostream out is open; subtreePtr points to a subtree 
     of this BST. 
    Postcondition: Subtree with root pointed to by subtreePtr has been 
     output to out. 
------------------------------------------------------------------------*/ 

    void graphAux(std::ostream & out, int indent, 
         BST<DataType>::BinNodePointer subtreeRoot) const; 
    /*------------------------------------------------------------------------ 
    Graph auxiliary function. 

    Precondition: ostream out is open; subtreePtr points to a subtree 
     of this BST. 
    Postcondition: Graphical representation of subtree with root pointed 
     to by subtreePtr has been output to out, indented indent spaces. 
------------------------------------------------------------------------*/ 

/***** Data Members *****/ 
    BinNodePointer myRoot; 

}; // end of class template declaration 

//--- Definition of constructor 
template <typename DataType> 
inline BST<DataType>::BST() 
: myRoot(0) 
{} 

//--- Definition of empty() 
template <typename DataType> 
inline bool BST<DataType>::empty() const 
{ return myRoot == 0; } 

//--- Definition of search() 
template <typename DataType> 
bool BST<DataType>::search(const DataType & item) const 
{ 
    BST<DataType>::BinNodePointer locptr = myRoot; //**THIS FAILS, WHY?**// 
    bool found = false; 
    while (!found && locptr != 0) 
    { 
     if (item < locptr->data)  // descend left 
     locptr = locptr->left; 
     else if (locptr->data < item) // descend right 
     locptr = locptr->right; 
     else       // item found 
     found = true; 
    } 
    return found; 
} 




#endif 

回答

2

聲明之前放置一個typename

typename BST<DataType>::BinNodePointer locptr = myRoot; 

的一點是,由於潛在的模板專業化,編譯器無法知道相關標識符BinNodePointer標識類型。