Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 7907395
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 3, 20262026-06-03T11:24:33+00:00 2026-06-03T11:24:33+00:00

//Node.cpp Node::Node(DataType Item):item(Item) { lchild = 0; rchild = 0; } DataType Node::getItem() {

  • 0
//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 & 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->getItem().getname()) == 0)
            return &(r->getItem());
        else
        {
            if (name.compare(r->getItem().getname()) < 0)
                return Search(name, r->getLChild());
            else
                return Search(name, r->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 << mc->getname() << endl;

The problem is at the MyClass object (mc) returned from Search function.

I trace into Search() and make sure “r->getItem()” get what I want.

anything wrong with “return &(r->getItem());” ?

Thanks!

++++++

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…

but I try your method to change the DataType* Node::getIthem()
it still have error….@@

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-03T11:24:35+00:00Added an answer on June 3, 2026 at 11:24 am

    I am guessing that Node::getItem() returns a DataType by value:

    DataType Node::getItem();
    

    When you take the address of that return value, you are essentially taking the address of something that will immediately disappear (a temporary). I suspect that Node holds DataType objects internally, in which case Node::getItem() could return a pointer to one of these.

    DataType* Node::getItem() { return &item; }
    const DataType* Node::getItem() const { return &item; }
    

    or return by reference:

    DataType& Node::getItem() { return item; }
    const DataType& Node::getItem() const { return item; }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

My problem is that i get this error binarytree.cpp: In member function ‘void BinaryTree<T>::printPaths(const
I have the following class class node { public: node() { } node(const node&);
struct node { Item item; node *l, *r; node(Item x) {item = x; l
class Node(object): def __init__(self, data): self.data = data if __name__ == __main__: a =
struct Node { int value Node* next; } typedef List Node* const Set operator
//node.h class node { public: void sort(node n); }; I didn't try the code
struct node { node(int _value, node* _next) : value(_value), next(_next) {} int value; node*
struct node { int id; float weight; }; int find_last(struct node Prio_Q[]) { int
Can node.js be setup to recognize a proxy (like Fiddler for example) and route
In Node.js is there any way to listen to all events emitted by an

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.