I am working on recreating the forward linked list class so I can better understand pointers. I have hit a roadblock, I have a template class called forward_list. Within this class in the private section I have another class which I want to have the same type as the main (external) class, this class is called node.
#ifndef FORWARD_LIST_HPP
#define FORWARD_LIST_HPP
template <class T>
class forward_list
{
private:
class node
{
T data;
node<T>* next;
};
node<T>* head, tail;
public:
forward_list();
~forward_list();
void pushBack(T t);
void print();
};
#endif
When I compile the above code with the rest of my code I produce this error:
./forward_list.hpp:11:19: error: non-template type ‘node’ used as a template
11 | node<T>* next;
|
I also have tried this (I will add a '*' on the line I have added.)
#ifndef FORWARD_LIST_HPP
#define FORWARD_LIST_HPP
template <class T>
class forward_list
{
private:
template <class T> // *
class node
{
T data;
node<T>* next;
};
node<T>* head, tail;
public:
forward_list();
~forward_list();
void pushBack(T t);
void print();
};
#endif
Here is the error this change has produced:
./forward_list.hpp:8:19: error: declaration of template parameter ‘T’ shadows template parameter
8 | template <class T>
|