Home How to link node objects in a data structure using templates?
Reply: 2

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

hidekiss
1#
hidekiss Published in 2018-02-12 17:32:58Z

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
Some programmer dude
2#
Some programmer dude Reply to 2018-02-12 17:35:22Z

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>.

ShOm
3#
ShOm Reply to 2018-02-12 17:36:59Z

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.

You need to login account before you can post.

About| Privacy statement| Terms of Service| Advertising| Contact us| Help| Sitemap|
Processed in 0.299936 second(s) , Gzip On .

© 2016 Powered by mzan.com design MATCHINFO