获取“Node”类型的临时对象的地址

时间:2015-11-01 15:29:28

标签: c++ tree xcode5 treenode preorder

t.PreorderTraversal(t, &t.getRoot());错误是获取“Node”类型的临时对象的地址。 Root是一个Node类对象。函数PreoderTraversal将Node对象作为一个点,因此我给出了Node对象的地址并发生了错误。这不是正确的方法吗?

class NodeList;

class Node {
private:
    Node* parent;
    int elem;
    NodeList* children;
    Node *next;
    Node *prev;

};

class NodeList {
public:
    NodeList();
    void addNodeAtRank(int, int);
private:
    Node* header;
    Node* tailer;
};

class Tree {
private:
    int n;
    Node root;
public:
    Tree();
    void addNode(Tree &t, int, int, int);
    void PreorderTraversal(const Tree& t, Node* p);
    void PostorderTraversal(const Tree& t, Node* p);
    void printXYofNode(const Tree& t, int nodeNumber);
    Node getRoot();
    Node* getNodeByElem(Node& n, int);
};

Node::Node() {

    children = nullptr;
    parent = nullptr;
    elem = 0;
    next = nullptr;
    prev = nullptr;

}

NodeList::NodeList() {

    header = new Node();
    tailer = new Node();

    header->next = tailer;
    tailer->prev = header;
}

void NodeList::addNodeAtRank(int rank, int e) {

    Node *v = new Node();
    v->elem = e;

    int count = 1;
    Node *NodeAtRank = header->next;

    while (count != rank) {
        NodeAtRank = NodeAtRank->next;
        count++;
    }

    v->next = NodeAtRank;
    v->prev = NodeAtRank->prev;
    NodeAtRank->prev = v;
    NodeAtRank->prev->next = v;

}

bool NodeList::empty() const {
    return header->next == tailer;
}

Tree::Tree() {

    n = 0;
    //root = Node();
}

void Tree::addNode(Tree& t, int NodeElement, int ParentNode, int SiblingOrder) {

    //Node *treeNode = new Node();

    if (t.empty() && ParentNode == -1 && SiblingOrder == -1) {
        t.root = Node();
        t.root.elem = NodeElement;
        t.root.children = new NodeList();
    } else {

        Node* nodeParent = t.getNodeByElem(t.root, ParentNode);

        NodeList *childrenNodelist = nodeParent->children;
        childrenNodelist->addNodeAtRank(SiblingOrder, NodeElement);

        nodeParent->children = childrenNodelist;
    }

    n++;
}

Node* Tree::getNodeByElem(Node& root, int nodeElem) {

    if (root.elem == nodeElem)
        return &root;
    else {
        NodeList *rootChildren = root.children;

        Node *head = rootChildren->header;

        while (head->next != rootChildren->tailer) {

            if (!head->next->isExternal())
                return getNodeByElem(*(head->next), nodeElem);
            else {
                if (head->next->elem == nodeElem)
                    return head->next;

                head = head->next;
            }
        }

        return new Node();
    }
}

void Tree::PreorderTraversal(const Tree& t, Node* p) {

    cout << p->elem;
    NodeList *mychildren = p->children;
    Node *traversal = mychildren->header->next;

    while (traversal != mychildren->tailer) {
        cout << " ";
        PreorderTraversal(t, traversal->next);
        traversal = traversal->next;
    }

}

void Tree::PostorderTraversal(const Tree& t, Node* p) {

    NodeList *mychildren = p->children;
    Node *traversal = mychildren->header->next;

    while (traversal != mychildren->tailer) {
        PreorderTraversal(t, traversal);
        traversal = traversal->next;
    }
    cout << p->elem;
}

bool Tree::empty() const {
    return n == 0;
}

int Tree::size() const {
    return n;
}

Node Tree::getRoot() {
    return root;
}

int main(int argc, const char * argv[]) {

    char Type = NULL;
    int nodeNumber = 0;
    int nodeParent = 0;
    int nodeOrderInSibling = 0;

    Tree t = Tree();
    cin >> Type;
    while (Type != 'Q') {
        if (Type == 'I') {
            cin >> nodeNumber >> nodeParent >> nodeOrderInSibling;
            t.addNode(t, nodeNumber, nodeParent, nodeOrderInSibling);
        } else if (Type == 'P') {
            t.PreorderTraversal(t, &t.getRoot());
        } else if (Type == 'T') {
            t.PostorderTraversal(t, &t.getRoot());
        } else if (Type == 'C') {
            cin >> nodeNumber;
            t.printXYofNode(t, nodeNumber);
        } else {
            cout << "Wrong input type!!!" << endl;
        }

        cin >> Type;
    }

    return 0;
}

3 个答案:

答案 0 :(得分:1)

此函数返回Node成员的Tree对象的副本

Node getRoot();

因此,在这一行中,您将获得此对象的地址,之后会被丢弃。

t.PreorderTraversal(t, &t.getRoot());

你留下的指针被称为悬空指针,因为它没有指向有效的对象。

考虑像这样修改getRoot

Node* Tree::getRoot() {
    return &root;
}

当然,您必须确保Root对象在使用此指针时不会超出范围

答案 1 :(得分:0)

编译错误完全正确。你 取一个临时的地址(这违反了标准)。 Tree :: getRoot()返回Node类的 copy ,因此&amp; t.getRoot()是临时的地址。我想你的意思是从getRoot()返回一个指针。其语法如下:

Node * getRoot();

答案 2 :(得分:0)

变化:

Node Tree::getRoot() {
    return root;
}

为:

Node& Tree::getRoot() {
    return root;
}

否则&t.getRoot() - 返回临时对象的地址, undefined behavior 非法。