2008-12-13 5 views
1

hI, j'essaye d'obtenir ce code du livre de Larry Nyhoff pour compiler dans Bloodshed. Il a été pris mot pour mot sur le site Web de l'auteur, bien que je l'ai déclaré sur .cpp au lieu de .h (le fichier .h ne fonctionne pas avec l'application du testeur).Obtenir ce modèle BST pour fonctionner

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

La fonction de recherche (const DataType & article) est ce qui me donne la douleur. L'erreur du compilateur dit:

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

Qu'est-ce qui me manque ici?

#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 

Répondre

2

Mettez un typename avant la déclaration:

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

Le point est qu'en raison de la spécialisation du potentiel, le compilateur ne peut pas savoir que l'identifiant dépendant BinNodePointer identifie un type.

Questions connexes