Thread: Template Class for Linked List

  1. #1
    Registered User
    Join Date
    Sep 2001

    Template Class for Linked List


    This is for an assignment I am trying to complete. For a previous assignment, I had to create a linked list class for integers. I was able to get that working fine. This assignment is to convert the linked list class into a template so that it can be a linked list of anything.

    I figured the best way to start was to take the linked list class for intergers, scale it back to just a few methods, and then convert it to a template.

    I converted my two classes. One was a Node class(which had a data member and a pointer to Node) and the other was Linked List (which had the pointer to the head node, a int member to keep track of the number of nodes in the list, and methods to pop, push, insert nodes). For the template version of the LinkedList class I just started with the push method and the constructors and destructors.

    With just the .h and .cpp files for the classes defined, the compiling works without error. But when I try to establish a variable of type LinkedList in a harness program I receive the following errors, but am uncertain how to resolve.

    --------------------Configuration: Assn5 Template Linked List Class - Win32 Debug--------------------
    Assn5_Linked List Class.cpp
    Assn5_Linked List Class.obj : error LNK2001: unresolved external symbol "public: virtual __thiscall LinkedList<int>::~LinkedList<int>(void)" (??1?$LinkedList@H@@UAE@XZ)

    Assn5_Linked List Class.obj : error LNK2001: unresolved external symbol "public: __thiscall LinkedList<int>::LinkedList<int>(void)" (??0?$LinkedList@H@@QAE@XZ)

    Debug/Assn5 Template Linked List Class.exe : fatal error LNK1120: 2 unresolved externals
    Error executing link.exe.

    Assn5 Template Linked List Class.exe - 3 error(s), 0 warning(s)


    I have attached the code I am using ...

  2. #2
    of Zen Hall zen's Avatar
    Join Date
    Aug 2001
    You'll either have to include the cpp file in your driver file or move all the template methods into the header file(a more common method). The compiler and not just the linker needs to be able to see these methods in the same file as the template instance to instantiate the template object.
    Last edited by zen; 12-03-2001 at 02:16 PM.

  3. #3
    Registered User
    Join Date
    Sep 2001
    Thanks, worked great

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Pleas take a look & give a critique
    By sh3rpa in forum C++ Programming
    Replies: 14
    Last Post: 10-19-2007, 10:01 PM
  2. Replies: 8
    Last Post: 07-24-2006, 08:14 AM
  3. Dikumud
    By maxorator in forum C++ Programming
    Replies: 1
    Last Post: 10-01-2005, 06:39 AM
  4. How can I traverse a huffman tree
    By carrja99 in forum C++ Programming
    Replies: 3
    Last Post: 04-28-2003, 05:46 PM
  5. Contest Results - May 27, 2002
    By ygfperson in forum A Brief History of
    Replies: 18
    Last Post: 06-18-2002, 01:27 PM