How to link node objects in a data structure using templates?

103 views Asked by At

I wanted to use templates to make more general data structures for example a stack by linking node* objects. But once I used the template the node class is no longer identified, the compiler says:

Error GCC template class Node’ declared here Error GCC invalid use of template-name ‘Node’ without an argument list

And there's my code on the header file so far:

#ifndef STACK_HPP
#define STACK_HPP
template <class Object>
class Node{
  friend class Stack;
  private:
    Object data;
    Node* next;
  public:
    Node(Object d);
    Node();
};
template <class Object>
class Stack{
  private:
    Node* top;
    int size;
    bool isEmpty();
  public:
    Stack();
    ~Stack();
    void Push(Object d);
    Object Pop();
    Object Spy();
};
#endif
2

There are 2 answers

0
Some programmer dude On

The compiler will not attempt to infer what Node might be in your Stack class, it's just a template. You need to provide a concrete class, as in Node<Object>.

0
JDoe On

Stack class is dependent upon Node class which is templated. So, wherever you declare any object of Node in Stack, you also have to provide of which type it has to be.