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....@@

2
  • what does Node::getItem() return? May 9, 2012 at 13:32
  • if getItem() returns MyClass (not MyClass&), then "return &(r->getItem());" returns an address to a temporary object, which is destroyed at the time of accessing it. Other returns should be blocked by the compiler.
    – stefaanv
    May 9, 2012 at 13:44

2 Answers 2

4

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; }
3
  • his getItem function is coded to return a copy of the data by value. The copy has to be removed as well, or he'll have the same issue. May 9, 2012 at 14:04
  • Thank you for solving my problem... it looks like I'd better to clarify the issue of pointer.... I'm confused why can't I use copy by value? May 10, 2012 at 10:45
  • @user1371541 you can use copy by value, but then you cannot assign the return value's address to a pointer or reference. You need a copy on the caller side. Think of the return value as a temporary that is then used to copy construct something else (although the most likely compiler optimizes away the copy, semantically there is a copy going on). May 11, 2012 at 12:20
0

return &(r->getItem()); will return the memory adress to whatever r->getItem() returns, not the object itself. If r->getItem() returns a pointer you will have to return (r->getItem());.

3
  • Unless of course r->getItem() returns a reference. Then he might be fine depending on how the map works.
    – user406009
    May 9, 2012 at 13:35
  • but if I change "r->getItem()" it will come up with "error C2440: 'return' : cannot convert from 'DataType' to 'DataType *' " Node.cpp is: DataType Node::getItem() const { DataType anItem = item; return anItem; } May 9, 2012 at 13:41
  • @user1371541: "DataType Node::getItem() const", see juanchpanza's answer
    – stefaanv
    May 9, 2012 at 13:56

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.