Note that there are some explanatory texts on larger screens.

plurals
  1. POI need assistance overloading the operator ==, <<,>> using trees
    primarykey
    data
    text
    <p>So far I have tried many things, but it is useless. I cannot seem to get any of my overloaded operator syntax or access correct. Can anyone please point me in the right direction of how to use these overloaded operators correctly? header file.</p> <pre><code>#include &lt;iostream&gt; #include &lt;fstream&gt; #include &lt;string&gt; using namespace std; #ifndef BINARY_SEARCH_TREE #define BINARY_SEARCH_TREE template &lt;typename DataType&gt; class BST { public: /***** Function Members *****/ BST(); /*------------------------------------------------------------------------ Construct a BST object. Precondition: None. Postcondition: An empty BST has been constructed. -----------------------------------------------------------------------*/ bool empty() const; /*------------------------------------------------------------------------ Check if BST is empty. Precondition: None. Postcondition: Returns true if BST is empty and false otherwise. -----------------------------------------------------------------------*/ bool search(const DataType &amp; item) const; /*------------------------------------------------------------------------ Search the BST for item. Precondition: None. Postcondition: Returns true if item found, and false otherwise. -----------------------------------------------------------------------*/ void insert(const DataType &amp; item); /*------------------------------------------------------------------------ Insert item into BST. Precondition: None. Postcondition: BST has been modified with item inserted at proper position to maintain BST property. ------------------------------------------------------------------------*/ void remove(const DataType &amp; item); /*------------------------------------------------------------------------ Remove item from BST. Precondition: None. Postcondition: BST has been modified with item removed (if present); BST property is maintained. Note: remove uses auxiliary function search2() to locate the node containing item and its parent. ------------------------------------------------------------------------*/ void inorder(ostream &amp; out) const; /*------------------------------------------------------------------------ Inorder traversal of BST. Precondition: ostream out is open. Postcondition: BST has been inorder traversed and values in nodes have been output to out. Note: inorder uses private auxiliary function inorderAux(). ------------------------------------------------------------------------*/ //OVER LOADED OPERATORS. bool operator==(const BST &amp; right)const; //Friend functions. friend std::ostream &amp; operator &lt;&lt;(std::ostream &amp; outs, const BST &amp; BinNode) {outs &lt;&lt; BinNode.Left()&lt;&lt; " " &lt;&lt; BinNode.right(); return outs;}; friend std::istream &amp; operator &gt;&gt;(std::istream&amp; ins, BST &amp; target) {ins &lt;&lt; target.left &lt;&lt; " " &lt;&lt; target.right; return ins;}; //Insertion of the file using a text tile. void readFile(); 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 of class BinNode declaration 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(ostream &amp; out, 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. ------------------------------------------------------------------------*/ /***** 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 { BinNodePointer locptr = myRoot; 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; } //--- Definition of insert() template &lt;typename DataType&gt; inline void BST&lt;DataType&gt;::insert(const DataType &amp; item) { BinNodePointer locptr = myRoot, // search pointer parent = 0; // pointer to parent of current node bool found = false; // indicates if item already in BST while (!found &amp;&amp; locptr != 0) { parent = locptr; 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; } if (!found) { // construct node containing item locptr = new BinNode(item); if (parent == 0) // empty tree myRoot = locptr; else if (item &lt; parent-&gt;data ) // insert to left of parent parent-&gt;left = locptr; else // insert to right of parent parent-&gt;right = locptr; } else cout &lt;&lt; "Item already in the tree\n"; } //--- Definition of remove() template &lt;typename DataType&gt; void BST&lt;DataType&gt;::remove(const DataType &amp; item) { bool found; // signals if item is found BinNodePointer x, // points to node to be deleted parent; // " " parent of x and xSucc search2(item, found, x, parent); if (!found) { cout &lt;&lt; "Item not in the BST\n"; return; } //else if (x-&gt;left != 0 &amp;&amp; x-&gt;right != 0) { // node has 2 children // Find x's inorder successor and its parent BinNodePointer xSucc = x-&gt;right; parent = x; while (xSucc-&gt;left != 0) // descend left { parent = xSucc; xSucc = xSucc-&gt;left; } // Move contents of xSucc to x and change x // to point to successor, which will be removed. x-&gt;data = xSucc-&gt;data; x = xSucc; } // end if node has 2 children // Now proceed with case where node has 0 or 2 child BinNodePointer subtree = x-&gt;left; // pointer to a subtree of x if (subtree == 0) subtree = x-&gt;right; if (parent == 0) // root being removed myRoot = subtree; else if (parent-&gt;left == x) // left child of parent parent-&gt;left = subtree; else // right child of parent parent-&gt;right = subtree; delete x; } //--- Definition of inorder() template &lt;typename DataType&gt; inline void BST&lt;DataType&gt;::inorder(ostream &amp; out) const { inorderAux(out, myRoot); } //--- Definition of search2() template &lt;typename DataType&gt; void BST&lt;DataType&gt;::search2(const DataType &amp; item, bool &amp; found, BinNodePointer &amp; locptr, BinNodePointer &amp; parent) const { locptr = myRoot; parent = 0; found = false; while (!found &amp;&amp; locptr != 0) { if (item &lt; locptr-&gt;data) // descend left { parent = locptr; locptr = locptr-&gt;left; } else if (locptr-&gt;data &lt; item) // descend right { parent = locptr; locptr = locptr-&gt;right; } else // item found found = true; } } //--- Definition of inorderAux() template &lt;typename DataType&gt; void BST&lt;DataType&gt;::inorderAux(ostream &amp; out, BinNodePointer subtreeRoot) const { if (subtreeRoot != 0) { inorderAux(out, subtreeRoot-&gt;left); // L operation out &lt;&lt; subtreeRoot-&gt;data &lt;&lt; " "; // V operation inorderAux(out, subtreeRoot-&gt;right); // R operation } } //---Overloading the Operator double equals. template &lt;typename DataType&gt; bool BST&lt;DataType&gt;::operator ==(const BST&amp; right) const { //Postcondition: The value returned is true if p1 and p2 // are identical; otherwise false returned. return (BinNodePointer.right == BinNodePointer.right) &amp;&amp; (BinNodePointer.left == BinNodePointer.left); } //tried to put all operations here to see a clean main with just function calls. template&lt;typename DataType&gt; void BST&lt;DataType&gt;::readFile() { BST&lt;string&gt; start; string data,motor; ifstream infile; infile.open("Tree.txt"); if (infile.fail( )) { cout &lt;&lt; "Input infile opening failed.\n"; exit(1); } getline(infile, data); while (! infile.eof( )) { start.insert(data); cout &lt;&lt; data &lt;&lt;endl; getline(infile, data); } cout&lt;&lt; "\n\nStarting a binary search tree.\n" &lt;&lt; "\nEnter the ID &amp; Password you wish to compare: "; /* if(start.operator==(motor)) cout &lt;&lt; "They are equal."; else cout &lt;&lt;"they are not equal."; */ //cout &lt;&lt; start.inorder(data); } #endif </code></pre> <p>This is my main.ccp, which I basically started testing after I wrote my overloaded operators, and since tweaking them so much, I spend about 2 days trying to figure out I couldn't access any member functions after tweaking. </p> <pre><code>#include&lt;iostream&gt; #include&lt;fstream&gt; #include"BST.h" using namespace std; int main() { BST&lt;string&gt; C; C.readFile(); C.empty(); C.insert("myself"); cout &lt;&lt; C; system("pause"); return 0; } </code></pre> <p>I have looked into operators examples of <code>==</code>, <code>&lt;&lt;</code>, <code>&gt;&gt;</code>, but I have never encounter anything much helpful using a binary search tree.</p> <p>like for example.</p> <p>I am trying to output what is already inside binary search tree with </p> <pre><code>cout &lt;&lt; C; </code></pre> <p>by using</p> <pre><code>friend std::ostream &amp; operator &lt;&lt;(std::ostream &amp; outs, const BST &amp; BinNode) {outs &lt;&lt; BinNode.Left&lt;&lt; " " &lt;&lt; BinNode.right; return outs;}; </code></pre> <p>this is the error i get from calling the ostream (cout&lt;&lt; C;) from main</p> <pre><code>Error 1 error C2039: 'Left' : is not a member of 'BST&lt;DataType&gt;' Error 2 error C2039: 'right' : is not a member of 'BST&lt;DataType&gt;' </code></pre> <p>also, from my readFile() function i am trying to make the operator ==, compare the incoming string to the string already inside the tree but it seems that i need to make the the operator a pointer to the class</p> <pre><code>template &lt;typename DataType&gt; bool BST&lt;DataType&gt;::operator ==(const BST&amp; right) const { //Postcondition: The value returned is true if p1 and p2 // are identical; otherwise false returned. return (BinNodePointer.right == BinNodePointer.right) &amp;&amp; (BinNodePointer.left == BinNodePointer.left); } </code></pre> <p>↑ this is what's killing me. I cannot seem to do the right comparison the text book that i used does not show me a very good example of it so that is i am asking for assistance.</p> <p>since i cannot post an answer i will just try to post here..</p> <p>When i tried to call the operator == inside readFile() function using this:</p> <pre><code>if(start.operator==(motor)) cout &lt;&lt; "They are equal."; else cout &lt;&lt;"they are not equal."; </code></pre> <p>i get an error of:</p> <pre><code>Error 1 error C2664: 'BST&lt;DataType&gt;::operator ==' : cannot convert parameter 1 from 'std::string' to 'const BST&lt;DataType&gt; &amp;' </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.
 

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