c++ - Why is my Linked List assignment not working as it should? -
so made linked list class:
class node { public: string name; node *next; node(string init) {name = init; next = nullptr; };
and in main created node pointer , initialized name of node points to.
node *root; root->name = "hello"; //error
the compiler doesn't call error, when run program stops working @ line of code. know why?
you need allocate node
object root
point to.
node* root = new node;
but careful, have make sure call delete
on root
@ right moment. coudl simplify things using smart pointer, or automatic storage object
node root; root.name = "hello";
Comments
Post a Comment