I am trying to write a function for a class "LinkedList" which has its own class "Node". The class "LinkedList" is a template class. I am trying to write a function "Foo_do_something" which returns a pointer to "Node". Following is the code:
template <typename T>
class LinkedList{
public:
class Node {
public:
T data;
Node *next;
Node *prev;
Node(): next(nullptr), prev(nullptr) {}
Node(const T & data): data(data), next(nullptr), prev(nullptr) {}
};
public:
Node* Foo_do_something();
};
template <typename T>
Node* LinkedList<T>::Foo_do_something(){
std::cout<<"Hello";
Node *ptr = new Node(100);
return ptr;
}
With this code I get the error:
error: ‘Node’ does not name a type
I understood that this is because "Node" belongs to class "LinkedList" scope. So I need to somehow add that information. I tried the following code but that also throws error.
LinkedList<T>::Node* LinkedList<T>::Foo_do_something(){
I can understand that if I define the function inside the class then the problem can be solved. But I was trying to keep the implementation of the function separate. Is there any way to do that?