templating a linked list

This is a discussion on templating a linked list within the C++ Programming forums, part of the General Programming Boards category; I have a doubly linked list class (and a node class). I've tested both their functions in main() and everything ...

  1. #1
    Registered User
    Join Date
    Apr 2010
    Posts
    4

    templating a linked list

    I have a doubly linked list class (and a node class). I've tested both their functions in main() and everything works the way it should.
    However, I'm now trying to template both classes and I'm having problems. I've read tutorials and spent a few days on this yet I can't seem to get it right. If someone could look over my code I would really appreciate it!

    the error i'm getting right now is in main.cpp when i create a DLList object for testing.

    main.cpp|43|undefined reference to DLList<int>:LList()

    DLList<int> testList;
    i've also tried DLList<DLNode> testList. I guess I'm not sure which to use (neither work though)


    http://dl.dropbox.com/u/3594001/DLNode.h
    http://dl.dropbox.com/u/3594001/DLList.h
    http://dl.dropbox.com/u/3594001/DLList.cpp
    http://dl.dropbox.com/u/3594001/main.cpp

  2. #2
    The larch
    Join Date
    May 2006
    Posts
    3,573
    You can't separate templates into header and implementation. Templates have to be fully available when instantiated (all in a header).
    I might be wrong.

    Thank you, anon. You sure know how to recognize different types of trees from quite a long way away.
    Quoted more than 1000 times (I hope).

  3. #3
    Registered User
    Join Date
    Apr 2010
    Posts
    4
    so everything in DLList.cpp needs to be in DLList.h?

  4. #4
    The larch
    Join Date
    May 2006
    Posts
    3,573
    Yes.

    Or you can keep it where it is and include "DLList.cpp" from the end of the header. (You shouldn't compile the file separately, though, and commonly it is given a different extension like "tpp" or "inl").
    I might be wrong.

    Thank you, anon. You sure know how to recognize different types of trees from quite a long way away.
    Quoted more than 1000 times (I hope).

  5. #5
    Registered User
    Join Date
    Apr 2010
    Posts
    4
    wow thanks guys. everything works now WOW lol... spent so much time on this.

    in my text book everything is seperated so that's why I did it that way. There's no include in the text book example though, but looking over it again there is a "usining namespace ..." in the cpp. Same effect?

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. C++ Linked list program need help !!!
    By dcoll025 in forum C++ Programming
    Replies: 1
    Last Post: 04-20-2009, 11:03 AM
  2. Following CTools
    By EstateMatt in forum C Programming
    Replies: 5
    Last Post: 06-26-2008, 11:10 AM
  3. Reverse function for linked list
    By Brigs76 in forum C++ Programming
    Replies: 1
    Last Post: 10-25-2006, 11:01 AM
  4. Template Class for Linked List
    By pecymanski in forum C++ Programming
    Replies: 2
    Last Post: 12-04-2001, 09:07 PM
  5. singly linked list
    By clarinetster in forum C Programming
    Replies: 2
    Last Post: 08-26-2001, 11:21 PM

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