Note that there are some explanatory texts on larger screens.

plurals
  1. POBinary Search Tree - search function return object (C++)
    primarykey
    data
    text
    <pre class="lang-cpp prettyprint-override"><code>//Node.cpp Node::Node(DataType Item):item(Item) { lchild = 0; rchild = 0; } DataType Node::getItem() { 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 DataType * BST::Search(const string name) { return Search(name, root); } DataType * BST::Search(const string name, Node * r) { if(r != 0) { if (name.compare(r-&gt;getItem().getname()) == 0) return &amp;(r-&gt;getItem()); else { if (name.compare(r-&gt;getItem().getname()) &lt; 0) return Search(name, r-&gt;getLChild()); else return Search(name, r-&gt;getRChild()); } } else return NULL; } //main.cpp MyClass mc1("Tree","This is a tree"); MyClass mc2("Book","This is a book"); MyClass mc3("Zoo","This is a zoo"); BST tree; tree.Insert(mc1); tree.Insert(mc2); tree.Insert(mc3); MyClass * mc = tree.Search("Book"); if (mc != NULL) cout &lt;&lt; mc-&gt;getname() &lt;&lt; endl; </code></pre> <p>The problem is at the MyClass object (mc) returned from Search function.</p> <p>I trace into Search() and make sure "r->getItem()" get what I want.</p> <p>anything wrong with "return &amp;(r->getItem());" ?</p> <p>Thanks!</p> <p>++++++</p> <p>I'm a little bit confused.. can I change to "DataType BST::Search(const string name)" instead of "DataType * BST::Search(const string name)"....it seems that the compiler cannot pass. the return NULL will have some problem...</p> <p>but I try your method to change the DataType* Node::getIthem() it still have error....@@</p>
    singulars
    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