1

t.PreorderTraversal(t, &t.getRoot()); The error is taking the address of a temporary object of type 'Node'. Root is a Node class object. The function PreoderTraversal will Node object as a point, so I give the address of the Node object and the error occurred. Isn't it the right way to do?

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;
}
Manos Nikolaidis
  • 21,608
  • 12
  • 74
  • 82
  • Consider indenting your code a bit better, it is very hard to read right now. –  Nov 01 '15 at 15:31
  • You traversal function prototypes are odd. Why are you taking const references to Tree objects as an argument? This requires that you specify the object *twice* during calls. `t.PreorderTraversal(t, &t.getRoot()); t.PostorderTraversal(t, &t.getRoot());` "this" is an implicit parameter in C++, so I think it would make more sense either to omit the Tree parameter (since it's not needed), or to declare the functions as static. – tweej Nov 01 '15 at 15:46

3 Answers3

1

This function returns a copy of the Node object that is a member of Tree

Node getRoot();

So in this line you are getting the address of this object that gets discarded right afterwards.

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

The pointer you are left with is called a dangling pointer as it doesn't point to a valid object.

Consider modifying getRoot like this

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

You must of course make sure that the Root object doesn't go out scope while you are using this pointer

Manos Nikolaidis
  • 21,608
  • 12
  • 74
  • 82
0

The compiler error is exactly right. You are taking the address of a temporary (and this is a violation of the standard). Tree::getRoot() returns a copy of class Node, so &t.getRoot() is the address of a temporary. I think you meant to return a pointer from getRoot() instead. The syntax for this would be:

Node * getRoot();
tweej
  • 832
  • 4
  • 16
0

Change:

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

to:

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

otherwise &t.getRoot() - is returning address of temporary object which is undefined behaviour illegal.

marcinj
  • 48,511
  • 9
  • 79
  • 100
  • +1 for steering him towards references instead of pointers in this case, but -1 because it's not undefined behavior. It's simply illegal. – tweej Nov 01 '15 at 15:52