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>
|
Your first example is pretty close to what you want. The thing to realize is that while
forward_list
is a class template,forward_list<T>
is a class, andforward_list<T>::node
is also a class, not a class template. But alsoforward_list<int>::node
is a totally separate class fromforward_list<double>::node
, even though they're both just callednode
.So, the following would work:
This way
forward_list<int>
will have a nested classforward_list<int>::node
with anint data;
member variable, andforward_list<double>
will have a nested classforward_list<double>::node
with adouble data;
member variable.