Difficulty compiling inherited template class

From: Greg (gregDOTdDOTsmith_at_gmail.com)
Date: 11/20/04


Date: Sat, 20 Nov 2004 13:11:35 -0800

As an exercise, I am writing a stack class (stack.h) that inherits from a
templatized double ended queue class (deque.h)
When I attempt to compile it, I get the following error (MSVC++ 7.1):

f:\school_projects\cs121\deque\main.cpp(47): error C2662: 'Deque<T>::~Deque'
: cannot convert 'this' pointer from 'Deque' to 'Deque<T> &'

If anyone has any ideas, I would appreciate the help... I have googled on
this error, but all the cases I found don't seem to apply to this case
particularly well.

thx
Greg

//Stack.h
#include "Deque.h"

template<class T>

class Stack : public virtual Deque

{

public:

Stack();

~Stack();

void Push(T item);

T Pop();

T* Peek();

int GetCount();

};

template< class T >

Stack<T>::Stack() : Deque()

{

}

template< class T >

Stack<T>::~Stack()

{

}

template< class T >

void Stack<T>::Push(T item)

{

Deque::EnqueueHead(item);

}

template< class T >

T Stack<T>::Pop()

{

return Deque::DequeueHead();

}

template< class T >

T* Stack<T>::Peek()

{

return Deque::PeekHead();

}

template< class T >

int Stack<T>::GetCount()

{

return Deque::GetCount();

}

//

// Deque.h
#include "QueueNode.h"

template<class T>

class Deque

{

public:

Deque();

~Deque();

void EnqueueHead(T item);

T DequeueHead();

T* PeekHead() const;

void EnqueueTail(T item);

T DequeueTail();

T PeekTail() const;

bool IsEmpty() const;

int GetCount() const;

private:

void SetFirstNode(QueueNode<T>*);

QueueNode< T >* ptrHead;

QueueNode< T >* ptrTail;

};

template< class T >

Deque<T>::Deque()

{

ptrHead = NULL;

ptrTail = NULL;

}

template <class T>

Deque<T>::~Deque

{

while(this->!IsEmpty())

{DequeueHead();}

}

template<class T>

void Deque<T>::EnqueueHead(T item)

{

QueueNode<T>* newNode = new QueueNode<T>(item);

if(IsEmpty())

{

SetFirstNode(newNode);

}

else

{

//set newNode's tail pointer to current head node

newNode->ToTail = ptrHead;

//set deque's head pointer to newNode

ptrHead->ToHead = newNode;

//Set newNode's ToHead pointer to null (redundant)

newNode->ToHead = NULL;

//set deque's head pointer to newNode

ptrHead = newNode;

}

}

template<class T>

T Deque<T>::DequeueHead()

{

if(IsEmpty())

{

return 0;

}

else

{

//get value of head node

T value = ptrHead->value;

//Get ref to node to be dequeued

QueueNode<T>* dqNode = ptrHead;

//set head node pointer to pointer of second node

ptrHead = ptrHead->ToTail;

//delete dequeued node

delete dqNode;

//if dequeued node was last node in deque, set tail pointer to null

if(ptrHead == NULL)

{

ptrTail = NULL;

}

return value;

}

}

template<class T>

T* Deque<T>::PeekHead() const

{

if(IsEmpty())

{

return 0;

}

else

{

return ptrHead->value;

}

}

template<class T>

void Deque<T>::EnqueueTail(T item)

{

QueueNode<T>* newNode = new QueueNode<T>(item);

if(IsEmpty())

{

SetFirstNode(newNode);

}

else

{

//Set new tail node's head pointer to current Deque's tail pointer

newNode->ToHead = ptrTail;

//Set deque's tail pointer's tail pointer to newnode

ptrTail->ToTail = newNode;

//set newnode's tail pointer to null (redundant)

newNode->ToTail = NULL;

//set Deque's tail pointer to newnode

ptrTail = newNode;

}

}

template<class T>

T Deque<T>::DequeueTail()

{

if(IsEmpty())

{

return 0;

}

else

{

//get value of tail node

T value = ptrTail->value;

//get ref to tail node

QueueNode<T>* dqNode = ptrTail;

//set new tail node as second node from tail

ptrTail = ptrTail->ToHead;

//delete dequeued node

delete dqNode;

//if dequeued node was last node in data structure, set head pointer to null

if(ptrTail == NULL)

{

ptrHead = NULL;

}

return value;

}

}

template<class T>

T Deque<T>::PeekTail() const

{

if(IsEmpty())

{

return 0;

}

else

{

return ptrTail->value;

}

}

template< class T >

bool Deque<T>::IsEmpty() const

{

return (ptrHead == NULL);

}

template< class T >

int Deque<T>::GetCount() const

{

int count = 0;

QueueNode* ptrNode = ptrHead;

while(ptrNode != NULL)

{

count++;

ptrNode = ptrNode->ToTail;

}

return count;

}

template< class T >

void Deque<T>::SetFirstNode(QueueNode<T>* newNode)

{

ptrHead = newNode;

ptrTail = newNode;

}

//

// QueueNode.h

template<class T>

class QueueNode

{

public:

QueueNode(T value);

T value;

QueueNode* ToHead;

QueueNode* ToTail;

};

template <class T>

QueueNode< T >::QueueNode(T value)

{

value = value;

ToHead = NULL;

ToTail = NULL;

}

//



Relevant Pages

  • Re: Item 42 of Effective C++
    ... > variable in the class are of the template type? ... several times does not lead to code bloat, and that is probably also not ... void pointer. ... believe at least knowing a bit of assembler helps a lot for understanding ...
    (alt.comp.lang.learn.c-cpp)
  • Re: The Decline of C/C++, the rise of X
    ... > tend to be most useful for encoding Boolean values in data structures ... to freea pointer obtained from the gc, ... >> to be a useful attribute of a language. ... Template arguments are evaluated in the scope of the point of instantiation, ...
    (comp.programming)
  • Re: null assignment in a template
    ... My question is about template programming ... and declare the value in its explicit ... An assignment operator taking an int and one ... taking a pointer? ...
    (microsoft.public.vc.stl)
  • Re: null assignment in a template
    ... // this template won't compile due to the fact that it is illegal to ... and declare the value in its explicit ... delcares a single ctor to force the client to have to declare an initial value for said variable, as oppossed to Inialisedwhich allows for a compile-time constant initialization value to be specified in the template parameters themselves. ... taking a pointer? ...
    (microsoft.public.vc.stl)
  • Table of pointers to templated functions
    ... How do I declare a typedef of a pointer to a templated ... template ... void print_name ...
    (comp.lang.cpp)