0

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 */
FloydVu0531
  • 23
  • 1
  • 5
  • I am not sure if the issue is but sometimes you should include `.cpp` file in your main. – Muhammedogz Jan 15 '21 at 03:11
  • @muhammedoğuz doesn't seem to work, and I just start writing my class and haven't implemented anything yet – FloydVu0531 Jan 15 '21 at 03:12
  • @RetiredNinja that doesn't seem to be the issue, as the code above compiles just fine on my machine. – Jeffrey Jan 15 '21 at 03:19
  • @Jeffrey Did you try to instantiate one? – Retired Ninja Jan 15 '21 at 03:19
  • Please add your `Node.h`. I made one and it compiles just fine for me. – Jeffrey Jan 15 '21 at 03:19
  • @RetiredNinja no, but that's beside the point. OP has an error on LinkedList.cpp, not an the instanciation point. (yeah, they'll get your issue down the road, but they're not there yet) – Jeffrey Jan 15 '21 at 03:21
  • @Jeffrey I just add my Node.h, maybe the problem starts from here? – FloydVu0531 Jan 15 '21 at 03:22
  • It works when i change from cpp to header, maybe because template can't be used in cpp file? – FloydVu0531 Jan 15 '21 at 03:24
  • Works for me as well. Maybe there is some problem with #ifdef/#define in your header? Some other file defines LinkedList_h? – monkeyman79 Jan 15 '21 at 03:28
  • Or there is some other LinkedList.hpp in your include path? – monkeyman79 Jan 15 '21 at 03:29
  • I just execute it on other compiler and it works fine too. The error occurs on Xcode – FloydVu0531 Jan 15 '21 at 03:30
  • 1
    'Closed - This question already has answers here:...' - that doesn't make sense – monkeyman79 Jan 15 '21 at 03:32
  • Note: including a cpp file leads to confusion. The build tool gets confused and compiles and tries to link it, leading to multiple definitions with the compiled cpp file and the files that included it. A co-worker gets confused and removes the include. The writer gets confused... Somebody gets confused. If you're not going to put the implementation inside the header with the definition, name the implementation pretty much anything but a .cpp file, .hpp, .impl, .tmpl, 42. It doesn't matter, but reserve .cpp for compiled source. – user4581301 Jan 15 '21 at 04:22

0 Answers0