Thread: linked list and template problem

  1. #1
    Registered User
    Join Date
    Apr 2006
    Posts
    25

    linked list and template problem

    To change a linked list into template, there's the code:

    template <class Items>
    Code:
    struct node
    {
      Items data;
      node *next;
    } NODE;
    Error message:template declaration of `struct node<Items> NODE'

    That said, I cannot keep the object NODE when doing a template.
    How to change those codes that involved the object NODE?
    Code:
    newHead=new(NODE);

  2. #2
    Registered User hk_mp5kpdw's Avatar
    Join Date
    Jan 2002
    Location
    Northern Virginia/Washington DC Metropolitan Area
    Posts
    3,817
    Code:
    template <class Items>
    struct node
    {
        Items data;
        node *next;
    } NODE;
    That is attempting to create an object of the node struct where the Items type is unknown at the time of compilation. You can't do that!

    Drop the NODE part. Elsewhere in your code you will be instantiating your node objects and at that time you specify the type, for example:
    Code:
    node<double> NODE;
    [edit]You may or may not need to change <class Items> to <typename Items> depending on what you're trying to do here.[/edit]


    [edit2]
    Code:
    node<double>* nodeptr = new node<double>;
    [/edit2]
    Last edited by hk_mp5kpdw; 04-03-2006 at 06:36 AM.
    "Owners of dogs will have noticed that, if you provide them with food and water and shelter and affection, they will think you are god. Whereas owners of cats are compelled to realize that, if you provide them with food and water and shelter and affection, they draw the conclusion that they are gods."
    -Christopher Hitchens

  3. #3
    Registered User
    Join Date
    Jan 2005
    Posts
    7,366
    >> You may or may not need to change <class Items> to <typename Items> depending on what you're trying to do here.

    That should never be required. There is no difference between the two other than style and clarity issues.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. singly linked circular list
    By DarkDot in forum C++ Programming
    Replies: 0
    Last Post: 04-24-2007, 08:55 PM
  2. error: template with C linkage
    By michaels-r in forum C++ Programming
    Replies: 3
    Last Post: 05-17-2006, 08:11 AM
  3. doubly linked list error.
    By noob2c in forum C++ Programming
    Replies: 12
    Last Post: 09-01-2003, 10:49 PM
  4. Linked list with two class types within template.
    By SilasP in forum C++ Programming
    Replies: 3
    Last Post: 02-09-2002, 06:13 AM
  5. Template Class for Linked List
    By pecymanski in forum C++ Programming
    Replies: 2
    Last Post: 12-04-2001, 09:07 PM