c++ - Taking the address of a temporary object of type 'Node' -
t.preordertraversal(t, &t.getroot());
error taking address of temporary object of type 'node'. root node class object. function preodertraversal
node object point, give address of node object , error occurred. isn't right way 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; }
this function returns copy of node
object member of tree
node getroot();
so in line getting address of object gets discarded right afterwards.
t.preordertraversal(t, &t.getroot());
the pointer left called dangling pointer doesn't point valid object.
consider modifying getroot
this
node* tree::getroot() { return &root; }
you must of course make sure root
object doesn't go out scope while using pointer
Comments
Post a Comment