-
Template Linked List
Hi all,
I am trying to make a couple of template classes for a generic linked list. I have gotting pretty far but are now stuct. I am getting errors from my add function where the first error is "undefined struct person", which is the data i want to add to the list. Anyway, i have atteched the files(code is not complete, but close). If any of you guys out are template experts, could you have a lock and give me an idea of how i should go about the whole thing.
Thanks in advance
G'n'R
-
The attachment compiles and appears to work. I've put it all in one file. If you are splitting it you'll probably have to put all the template implementation into the header file.
-
Cool, thanks Zen. It works, but gives an error on exit. But i will figure that one out.
Cheers
G'n'R
-
I changed my code and it works perfectly except for an error on exit. I have localized it to be in the destructer, but the error goes away if i add the node differently. e.g
Code:
/* if i do it like this, there is no error in the free function in the class destructer */
if(!(nodeToAdd = new _node<_data>()))
/* however if i do it like this, it will generate an error in the class destructers free function */
if((nodeToAdd = (_node<_data>*)malloc(sizeofdata))==NULL)
in the destructor i just loop and use free(node), but it will generate a Debug Error, Damage after block etc. if i use the malloc version.
Just wondering, have you got any thought on that?
G'n'R
-
When using classes you have to allocate them with the new keyword or the constructor is not called and you have to delete them not free them or the deconstructor is not called.
Also I wouldn't begin my identifiers with an underscore('_') because ANSI reserves those for compiler writers and you may overwrite a variable that the compiler writer used... just a suggestion though :p
-
When using C++ stick to new and delete. Agrajag explaned why.
-
Cool, never new that. Thanks
G'n'R