简体   繁体   English

二进制搜索树-搜索函数返回对象(C ++)

[英]Binary Search Tree - search function return object (C++)

//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. 问题出在从搜索功能返回的MyClass对象(mc)上。

I trace into Search() and make sure "r->getItem()" get what I want. 我跟踪Search()并确保“ r-> getItem()”得到我想要的。

anything wrong with "return &(r->getItem());" “返回&(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. 我有点困惑..我可以改为“ DataType BST :: Search(常量字符串名称)”而不是“ DataType * BST :: Search(常量字符串名称)” ....似乎编译器无法通过。 the return NULL will have some problem... 返回NULL将有一些问题...

but I try your method to change the DataType* Node::getIthem() it still have error....@@ 但我尝试使用您的方法来更改DataType * Node :: getIthem(),但仍然有错误。...@@

I am guessing that Node::getItem() returns a DataType by value: 猜想 Node::getItem()通过值返回一个DataType

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. 我怀疑Node在内部保存了DataType对象,在这种情况下, Node::getItem()可能返回指向其中之一的指针。

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; }

return &(r->getItem()); will return the memory adress to whatever r->getItem() returns, not the object itself. 会将内存地址返回到r->getItem()返回的内容,而不是对象本身。 If r->getItem() returns a pointer you will have to return (r->getItem()); 如果r->getItem()返回一个指针,则必须return (r->getItem()); .

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM