I'm writing a small template class that create and display LinkedList. I also have a class Node that writes in a different file(Node.h). I write the LinkedList class definition in the cpp file, which cause undefine template error in the default constructor.
LinkedList.hpp
#ifndef LinkedList_h
#define LinkedList_h
#include "Node.h"
template<class T>
class LinkedList
{
private:
Node<T> *headPtr;
int data;
public:
LinkedList();
LinkedList(T A[], int n);
virtual ~LinkedList();
};
#endif
LinkedList.cpp
#include "LinkedList.hpp"
template<class T>
LinkedList<T>::LinkedList() //ERROR: No template named "LinkedList"
{
headPtr = nullptr;
data = 0;
}
Do I forget to include something?
//UPDATE: I'm adding my Node.h
#ifndef Node_h
#define Node_h
// --------------- class Prototype ----------------- //
template<class T>
class Node
{
private:
T item;
Node<T>* next;
public:
Node();
Node(const T& item);
Node(const T& item, Node<T> * next);
void setItem(const T& item);
void setNext(Node<T>* nextNodePtr);
T getItem() const;
Node<T>* getNext() const;
};
// ------------ Member ------------- //
//Default Constructor
template<class T>
Node<T>:: Node() : next(nullptr) {}
//End default constructor
//COPY CONSTRUCTOR1
template<class T>
Node<T>:: Node(const T& item)
{
this->item = item;
next = nullptr;
}
//END COPY CONSTRUCTOR1
//Copy Constructor2
template<class T>
Node<T>:: Node(const T& item, Node<T> *nextNodePtr)
{
this->item = item;
next(nextNodePtr);
}
//END COPY CONSTRUCTOR2
//MUTATOR
template<class T>
void Node<T>::setItem(const T& Item)
{
this->item = item;
} //END SET ITEM
template<class T>
void Node<T>::setNext(Node<T>* nextNodePtr)
{
next(nextNodePtr);
}//END SET NEXT
//ACCESOR
template<class T>
T Node<T>::getItem() const
{
return item;
}
template<class T>
Node<T>* Node<T>::getNext() const
{
return next;
}
#endif /* Node_h */