40

I am facing a problem with my linked list class, I have created the interface and implementation files of the class, but when I build it, this error occurs: "invalid use of template name 'LinkedList' without an argument list". here's my interface file:

#ifndef LINKEDLIST_H
#define LINKEDLIST_H

template <typename T>
struct Node{
    T info;
    Node<T> *next;
};

template <typename T>
class LinkedList
{
    Node<T> *start;
    Node<T> *current;
public:
    LinkedList();
    ~LinkedList();
};

#endif // LINKEDLIST_H

and this is my implementation code:

#include "LinkedList.h"

LinkedList::LinkedList()
{
   start = nullptr;
   current = nullptr;
}

LinkedList::~LinkedList()
  {

  }
Cœur
  • 37,241
  • 25
  • 195
  • 267
Alladin
  • 1,010
  • 3
  • 27
  • 45

1 Answers1

70

Write it like this:

template<typename T>
LinkedList<T>::LinkedList()
{
   start = nullptr;
   current = nullptr;
}

And similarly for other member functions. But you'll run into another problem - declarations and definitions of a template can't be separated to different files.

Community
  • 1
  • 1
jrok
  • 54,456
  • 9
  • 109
  • 141
  • I have done it, but as you said, there is an error with the declaration of 'NULL', it says that NULL was not declared in this scope. how can I solve this issue?? – Alladin Aug 12 '13 at 12:47
  • @Alladinsaoudi NULL is a macro that's defined in several different headers listed [here](http://en.cppreference.com/w/cpp/types/NULL). You'll have to include at least one of them where you want to use it. – jrok Aug 12 '13 at 12:50
  • @jrol thanks a looooooooooot , i do really appreciate your help. :)))) – Alladin Aug 12 '13 at 12:56
  • 7
    @Alladinsaoudi With C++11 better use `nullptr` instead of `NULL`. Moreover, you can always simply use `0` instead of `NULL` (but `nullptr` is better as the compiler can make some checks). – Walter Aug 12 '13 at 13:20
  • 1
    Anyone using C++11 or above should now use `nullptr`. – Isaac Woods Oct 18 '15 at 09:39
  • Maybe it is worth mentioning that sometimes the library already provides a specialization for the class you want to use, like a `Vector3` that could be used as `Vector3` or `Vector3d` – ignacio Mar 25 '21 at 13:32