Scuba Steve
Scuba Steve

Reputation: 1648

problems inheriting from a c++ template class

So I'm trying to work out how inheritance works when templates are in the mix. Most compilers really don't seem to have this figured out yet, so I'm having a little syntax difficulty. All the weird includes in SkipNode.h are from trying to get eclipse to stop yelling at me. I'm getting a syntax error when trying to declare the constructor in SkipNode.h, so any help here would be useful.

Here is node.h

#ifndef NODE_H_
#define NODE_H_

template<class T>
class Node
{
public:

    Node(Node<T>* next, Node<T>* prev, T item);

    virtual ~Node();

    Node* getPrev() { return prev;};

    Node* getNext() { return next;};

    Node* getItem() { return item;};

    void setItem(T item){Node<T>::item = item;};

    void setNext(Node* next){Node<T>::next = next;};

    void setPrev(Node* prev){Node<T>::prev = prev;};
private:
    Node* next;
    Node* prev;
    T item;
};

Here is SkipNode.h, where skipnode inherits from Node.

#include "Node.h"
#include "Node.cpp"
#include "SkipNode.h"
#include "SkipNode.cpp"

template <class T>
class SkipNode: public Node
{
public:
    SkipNode(Node<T>* next, Node<T>* prev, Node<T>* child, T item) : Node(next, prev, item);

    virtual ~SkipNode();

    Node* getChild(){return child;};

    void setChild(Node* child){SkipNode::child = child;};

private:
    Node *child;
};

#endif /* SKIPNODE_H_ */

Upvotes: 3

Views: 324

Answers (2)

Dan O
Dan O

Reputation: 4450

You're missing an #endif in node.h and you need to provide the template parameter to your base class:

template <class T>
class SkipNode : public Node< T >
....

This level of indirection exists to allow inheritance from a template class by a class that has different (or zero) template parameters. For instance:

class Foo : public Node< int >

Upvotes: -1

billz
billz

Reputation: 45410

Node is a template, you should pass in template parameter

template <class T>
class SkipNode: public Node<T>
//                         ^^^

Also you need to provide SkipNode constructor definition as you have provided member iniatilizer list.

update:

SkipNode(Node<T>* next, Node<T>* prev, Node<T>* child, T item) 
: Node(next, prev, item);

To:

SkipNode(Node<T>* next, Node<T>* prev, Node<T>* child, T item)
: Node(next, prev, item)
{
}

Upvotes: 5

Related Questions