c++ - Queue implementation is losing track of head node -
i have queue add function implemented
void queue::add(myobj info) { node* node = new node; node->info = &info; //<---suspect node->next = null; if(head == null){ head = node; } else{ tail->next = node; } tail = node; count++; }
every time gets visited head node's data points whatever i'm passing in. realize there template trying build one, because need practice.
i trying keep pointers pointed original objects. wanted pass in object , point refrence.
the node struct myobj * info
, node * next
info
parameter of function, passed value. in case, &info
address of parameter, , not of original data.
this undefined behaviour , can give weird results.
one possible solution be:
void queue::add(myobj& info) // pass reference { ... // unchanged code }
in case, &info
refer address of original object.
Comments
Post a Comment