0

// This is the Node.h file

#ifndef NODE
#define NODE                                                                            

template <typename T>
class Node
{
   private:
   T elem;
   Node *next;
   friend class LinkedList<T>;
};

#endif // NODE

This is the LinkedLilst.h file

#ifndef LINKED_LIST
#define LINKED_LIST

#include "Node.h"

template <typename T>
class LinkedList
{
public:
  LinkedList();
  ~LinkedList();
  bool empty() const;
  const T &front() const;
  void addFront(const T &e);
  void removeFront();

private:
  Node<T> *head;
};

#endif // LINKED_LIST

This is the LinkedList.cpp file

#include <iostream>
#include "LinkedList.h"
using namespace std;

template <typename T>
LinkedList<T>::LinkedList() : head(NULL) {}

template <typename T>
bool LinkedList<T>::empty() const // I don't want it to modify the data member of the function.
{
  return head == NULL;
}

template <typename T>
LinkedList<T>::~LinkedList()
{
  while (!empty())
    removeFront(); 
}
...
...
...


This is my main.cpp file

#include <iostream>
#include "LinkedList.h"
using namespace std;
int main()
{
  LinkedList<int> ls;
  ls.addFront(3);
  cout << ls.front();
  return 0;
}

I don't know why I am getting the error: 'LinkedList' is not a class template

   friend class LinkedList<T>; in Node.h

The problem is that Node.h file doesn't have anything related to LinkedList. I added LinkedList Declaration but it still showing errors. Please help.

Tk123
  • 3
  • 2
  • If you fixed that, take a look [here](https://stackoverflow.com/questions/495021/why-can-templates-only-be-implemented-in-the-header-file) please. – πάντα ῥεῖ Oct 10 '21 at 20:01
  • My 2 cents are that Nodes are an implementation detail of the linked list, and should be privately declared in the linked list class. There’s no reason for Nodes to be available for someone to declare Node objects. – sweenish Oct 10 '21 at 21:01

1 Answers1

1

You need to forward declare the LinkedList class template:

#ifndef NODE
#define NODE                                                                            

template<class> class LinkedList;   // <- forward declaration

template <typename T>
class Node
{
   private:
   T elem;
   Node *next;
   friend class LinkedList<T>;
};

#endif // NODE

The next problem you are going to run in to is probably a linking problem. I suggest moving the class member functions definitions into the header files. More on that here: Why can templates only be implemented in the header file?

Ted Lyngmo
  • 93,841
  • 5
  • 60
  • 108
  • Thank you, sir, for the reply but now it is showing another error: undefined reference to `LinkedList::LinkedList() and undefined reference to `LinkedList::addFront(int const&) and so on. – Tk123 Oct 10 '21 at 20:06
  • @Tk123 You're welcome. You should implement the class template member functions in the header file where you define the class template. That's what the note and link I left at the bottom of the answer deals with. – Ted Lyngmo Oct 10 '21 at 20:07
  • Sir, When I am including the LinkedList.cpp file along with the LinkedList.h, its not showing any error and working fine. – Tk123 Oct 10 '21 at 20:11
  • @Tk123 Yes, you are then treating the `.cpp` file as a header file. That's just going to confuse readers. Just move what you have in the `.cpp` into the header file and it'll be understandable by other programmers. – Ted Lyngmo Oct 10 '21 at 20:13
  • Thank you, it worked. If you could provide any article as to why this happens when we use class templates, would be a great help. – Tk123 Oct 10 '21 at 20:28
  • @Tk123 Great, you're welcome! I think the link I put in the answer leads to some good explanations. – Ted Lyngmo Oct 10 '21 at 20:31