Note that there are some explanatory texts on larger screens.

plurals
  1. POGetting this BST template to work
    primarykey
    data
    text
    <p>hI, I'm trying to get this code from Larry Nyhoff's book to compile in Bloodshed. It's actually been taken word for word from the author's website, though I declared it on .cpp instead of .h (the .h file ain't working with the tester application).</p> <p><a href="http://cs.calvin.edu/activities/books/c++/ds/2e/SourcePrograms/Chap12/" rel="nofollow noreferrer">http://cs.calvin.edu/activities/books/c++/ds/2e/SourcePrograms/Chap12/</a> </p> <p>The search(const DataType &amp; item) function is what's giving me grief. The compiler error says:</p> <pre><code>In member function `bool BST&lt;DataType&gt;::search(const DataType&amp;) const': expected `;' before "locptr" `locptr' undeclared (first use this function) </code></pre> <p>What I'm I missing here?</p> <pre><code> #include &lt;iostream&gt; #ifndef BINARY_SEARCH_TREE #define BINARY_SEARCH_TREE template &lt;typename DataType&gt; class BST { public: /***** Function Members *****/ BST(); bool empty() const; bool search(const DataType &amp; item) const; void insert(const DataType &amp; item); void remove(const DataType &amp; item); void inorder(std::ostream &amp; out) const; void graph(std::ostream &amp; 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 &amp; item, bool &amp; found, BinNodePointer &amp; locptr, BinNodePointer &amp; 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 &lt;iostream&gt; ------------------------------------------------------------------------*/ void inorderAux(std::ostream &amp; out, BST&lt;DataType&gt;::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 &amp; out, int indent, BST&lt;DataType&gt;::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 &lt;typename DataType&gt; inline BST&lt;DataType&gt;::BST() : myRoot(0) {} //--- Definition of empty() template &lt;typename DataType&gt; inline bool BST&lt;DataType&gt;::empty() const { return myRoot == 0; } //--- Definition of search() template &lt;typename DataType&gt; bool BST&lt;DataType&gt;::search(const DataType &amp; item) const { BST&lt;DataType&gt;::BinNodePointer locptr = myRoot; //**THIS FAILS, WHY?**// bool found = false; while (!found &amp;&amp; locptr != 0) { if (item &lt; locptr-&gt;data) // descend left locptr = locptr-&gt;left; else if (locptr-&gt;data &lt; item) // descend right locptr = locptr-&gt;right; else // item found found = true; } return found; } #endif </code></pre>
    singulars
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload