Note that there are some explanatory texts on larger screens.

plurals
  1. POSomething weird with operator overloading (C++)
    primarykey
    data
    text
    <pre><code>###MyClass.h### #ifndef _MyClass #define _MyClass #include &lt;string&gt; using namespace std; class MyClass { public: MyClass(const string name, const string text); void display(ostream &amp; out) const; MyClass &amp; operator = (const MyClass &amp; m); int compare(const MyClass &amp; right) const; private: string _name; string _text; }; bool operator &lt; (const MyClass &amp; left, const MyClass &amp; right); ostream &amp; operator &lt;&lt; (ostream &amp; out, const MyClass &amp; mc); #endif ###Node.h### #include &lt;string&gt; #include "MyClass.h" using namespace std; typedef MyClass * DataType; class Node { private: DataType item; // data Node * lchild; // left child pointer Node * rchild; // right child pointer public: Node(DataType Item); DataType getItem() const; void setItem(const DataType &amp; data); Node* getLChild() const; void setLChild(Node * p); Node* getRChild() const; void setRChild(Node * p); virtual ~Node(); }; ###BST.h### #include "Node.h" using namespace std; class BST { private: Node * root; bool Search(const DataType item, Node * r) const; void Insert (DataType item, Node * ptr); void Destructor(const Node * r); public: BST(); bool IsEmpty() const; void Insert(const DataType item); bool Search(const DataType item) const; virtual ~BST(); }; ###MyClass.cpp### #include &lt;iostream&gt; #include "MyClass.h" using namespace std; MyClass::MyClass(const string name, const string text) { _name = name; _text = text; } void MyClass::display(ostream &amp; out) const { out &lt;&lt; "Name: " &lt;&lt; _name &lt;&lt; endl; out &lt;&lt; "Text: " &lt;&lt; _text &lt;&lt; endl; } MyClass &amp; MyClass::operator = (const MyClass &amp; m) { if (this == &amp; m) return *this; _name = m._name; _text = m._text; return *this; } int MyClass::compare(const MyClass &amp; right) const { return _name.compare(right._name); } bool operator &lt; (const MyClass &amp; left, const MyClass &amp; right) { return left.compare(right) &gt; 0; } ostream &amp; operator &lt;&lt; (ostream &amp; out, const MyClass &amp; mc) { mc.display(out); return out; } ###Node.cpp### #include "Node.h" Node::Node(DataType Item):item(Item) { lchild = 0; rchild = 0; } DataType Node::getItem() const { DataType anItem = item; return anItem; } void Node::setItem( const DataType &amp; data) { item = data; } Node* Node::getLChild() const { Node * p = lchild; return p; } void Node::setLChild(Node * p) { lchild = p; } Node* Node::getRChild() const { Node * p = rchild; return p; } void Node::setRChild(Node * p) { rchild = p; } Node::~Node() { } ###BST.cpp### #include &lt;iostream&gt; #include "BST.h" using namespace std; bool BST::Search(const DataType item) const { return Search(item, root); } bool BST::Search(const DataType item, Node * r) const { if(r != 0) { if (item == r-&gt;getItem()) return true; else { if (item &lt; r-&gt;getItem()) return Search(item, r-&gt;getLChild()); else return Search(item, r-&gt;getRChild()); } } else return false; } BST::BST() { root = 0; } bool BST::IsEmpty() const { return (root == 0); } void BST::Insert(const DataType item) { if(root == 0) root = new Node(item); else Insert(item, root); } void BST::Insert(DataType item, Node * ptr) { if (item &lt; ptr-&gt;getItem()) { if (ptr-&gt;getLChild() == 0) ptr-&gt;setLChild(new Node(item)); else Insert(item, ptr-&gt;getLChild()); } else { if (ptr-&gt;getRChild() == 0) ptr-&gt;setRChild(new Node(item)); else Insert(item, ptr-&gt;getRChild()); } } void BST::Destructor(const Node * r) { if(r!=0) { Destructor( r-&gt;getLChild()); Destructor( r-&gt;getRChild()); delete r; } } BST::~BST() { Destructor(root); } ###main.cpp### #include &lt;iostream&gt; #include "MyClass.h" #include "BST.h" using namespace std; void main() { MyClass * mc1 = new MyClass("Tree","This is a tree"); MyClass * mc2 = new MyClass("Book","This is a book"); MyClass * mc3 = new MyClass("Zoo","This is a zoo"); BST * tree = new BST(); tree-&gt;Insert(mc1); tree-&gt;Insert(mc2); tree-&gt;Insert(mc3); cout &lt;&lt; boolalpha &lt;&lt; ("Book" &lt; "Tree") &lt;&lt; endl; cout &lt;&lt; (mc2 &lt; mc1) &lt;&lt; endl; cout &lt;&lt; (tree-&gt;Search(new MyClass("Book",""))) &lt;&lt; endl; } </code></pre> <blockquote> <p>Result is true false false</p> <ol> <li>I don't know what's wrong with my operator overloading? (mc2 should less than mc1)</li> <li>I'm not sure if this is correct for searching a "MyClass" node in a BST? and the result is "not found"....I traced it into "BST.cpp", and found that the problem also occurs at " if (item &lt; r->getItem()) "</li> </ol> <p>Can anyone help me or give me a hint....thank you so much!</p> </blockquote>
    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