for struct node do I need to do this?

1
2
3
4
5
6
7
8
9
template <class item>
struct node {
    node * next;
    item data;
    node(){ }
    ~node(){ 
              delete next;
    }
};


Because I'm getting runtime erros when I try to delete data member next. Even if I do something like:
1
2
if (next != NULL)
   delete next;


it will fail.
Well, you can't delete something that doesn't exist.
next needs to point either to 0 or to a valid address where a node object was previously constructed using new.
if I have an instance of myqueue<int>, and I call delete specifically on that instance of myqueue, will my myqueue destructors need to call delete specifically on myqueues members, because if the memory allocated for myqueue is freed, then shouldn't all the space requried for its data members be freed as well by default?
Last edited on
Of course. You can't use delete on non-pointers anyway.
Every new needs a delete. If you didn't use new to create an object, you can't delete it.
your problem is that you didn't set 'next' to NULL in the constructor (like node() : next(NULL) { }). So 'next' contains an undefined value and that if (next != NULL) doesn't help
Topic archived. No new replies allowed.