C++ Templates - LinkedList

c++, linked-list, templates

Solution

Might wanna try

Node<T>* temp = new Node<T>;

Also, to get hints on how to design the list, you can of course look at std::list, although it can be a bit daunting at times.

Problem

EDIT -- Answered below, missed the angled braces. Thanks all. I have been attempting to write a rudimentary singly linked list, which I can use in other programs. I wish it to be able to work with built-in and user defined types, meaning it must be templated. Due to this my node must also be templated, as I do not know the information it is going to store. I have written a node class as follows - ``` template <class T> class Node { T data; //the object information Node* next; //pointer to the next node element public: //Methods omitted for brevity }; ``` My linked list class is implemented in a seperate class, and needs to instantiate a node when adding new nodes to the end of the list. I have implemented this as follows - ``` #include <iostream> #include "Node.h" using namespace std; template <class T> class CustomLinkedList { Node<T> *head, *tail; public: CustomLinkedList() { head = NULL; tail = NULL; } ~CustomLinkedList() { } //Method adds info to the end of the list void add(T info) { if(head == NULL) //if our list is currently empty { head = new Node<T>; //Create new node of type T head->setData(info); tail = head; } else //if not empty add to the end and move the tail { Node* temp = new Node<T>; temp->setData(info); temp->setNextNull(); tail->setNext(temp); tail = tail->getNext(); } } //print method omitted }; ``` I have set up a driver/test class as follows - ``` #include "CustomLinkedList.h" using namespace std; int main() { CustomLinkedList<int> firstList; firstList.add(32); firstList.printlist(); //Pause the program until input is received int i; cin >> i; return 0; } ``` I get an error upon compilation however - error C2955: 'Node' : use of class template requires template argument list - which points me to the following line of code in my add method - ``` Node* temp = new Node<T>; ``` I do not understand why this has no information about the type, since it was passed to linked list when created in my driver class. What should I be doing to pass the type information to Node? Should I create a private node struct instead of a seperate class, and combine the methods of both classes in one file? I'm not certain this would overcome the problem, but I think it might. I would rather have seperate classes if possible though. Thanks, Andrew.

Original source