Template Linked List

This is a discussion on Template Linked List within the C++ Programming forums, part of the General Programming Boards category; Hi all, I am trying to make a couple of template classes for a generic linked list. I have gotting ...

  1. #1
    Registered User
    Join Date
    Aug 2001
    Posts
    55

    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
    Attached Files Attached Files

  2. #2
    zen
    zen is offline
    of Zen Hall zen's Avatar
    Join Date
    Aug 2001
    Posts
    1,007
    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.
    Attached Files Attached Files
    zen

  3. #3
    Registered User
    Join Date
    Aug 2001
    Posts
    55
    Cool, thanks Zen. It works, but gives an error on exit. But i will figure that one out.

    Cheers
    G'n'R

  4. #4
    Registered User
    Join Date
    Aug 2001
    Posts
    55
    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

  5. #5
    Registered User
    Join Date
    Dec 2001
    Posts
    26
    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
    one fish two fish
    red fish blue fish

  6. #6
    Registered User
    Join Date
    Dec 2001
    Posts
    194
    When using C++ stick to new and delete. Agrajag explaned why.

  7. #7
    Registered User
    Join Date
    Aug 2001
    Posts
    55
    Cool, never new that. Thanks

    G'n'R

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Anyone good with linked list.....I am not....
    By chadsxe in forum C++ Programming
    Replies: 11
    Last Post: 11-10-2005, 01:48 PM
  2. Replies: 3
    Last Post: 03-04-2005, 01:46 PM
  3. Linked List
    By silicon in forum C++ Programming
    Replies: 5
    Last Post: 07-21-2004, 07:47 PM
  4. Template Class for Linked List
    By pecymanski in forum C++ Programming
    Replies: 2
    Last Post: 12-04-2001, 08:07 PM
  5. singly linked list
    By clarinetster in forum C Programming
    Replies: 2
    Last Post: 08-26-2001, 10:21 PM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21