Hello all,

So I'm working on this simple program with a Queue class template that is publicly derived from the abstract base class CharQueue.

The only problem is that I keep getting hit with the error "expected class name before '{' token". The reason I don't understand this is that I never got this error until I made the class a template, but I can't see how it being a template now should affect that.

Anyways, here's the code (the error is at line 8/9:

queueTemplate.h containing Queue class:

#include "CharQueueTemplate.h"
#include <cassert>
#include <new>
#include <cstddef>

using namespace std;

template <typename T> class Queue : public CharQueue
{
    private:
    struct qNode {
    T element;
    qNode *next;
    };//end qNode
    qNode *front;
    qNode *back;


    public:

    Queue();//default constructor
    Queue(const Queue<T>& q);//copy constructor
    virtual ~Queue();//destructor

    virtual bool isEmpty() const;//checks to see if the queue is empty

    virtual void enqueue(const T & value);//places value at the back of the queue (prior to the head pointer in this case)

    virtual void dequeue();//removes the front value from the queue

    //virtual void dequeue(T& head);

    virtual T getFront() const;

};//end Queue


template <typename T> Queue<T>::Queue()
{
    back= NULL;
    front = NULL;

}//end default constructor


template <typename T> Queue<T>::Queue(const Queue & aq)
{
    if(!aq.isEmpty())
    {
        qNode *original = aq.front;
        qNode *newhead = new qNode;//points to the head of the list
        newhead->element = original->element;
        newhead->next = newhead;
        front = newhead;

        qNode *nCurrent = newhead;
        //qNode *temp = new qNode;

        while(original != aq.back)
        {
            original = original->next;
            nCurrent->next= new qNode;
            nCurrent = nCurrent->next;
            nCurrent->element = original->element;
            nCurrent->next = NULL;
            back=nCurrent;//possible error
        }//end while

    }//end if

}//end copy constructor

template <typename T> Queue<T>::~Queue()
{
    while(!isEmpty())
    {
        dequeue();
    }
 }//end destructor

template <typename T> bool Queue<T>::isEmpty() const
{
    return (front == NULL);
}//end isEmpty

template <typename T> void Queue<T>::enqueue(const T& value)
{
    try
    {
        //make new node
        qNode *temp = new qNode;

        temp->element = value;
        temp->next = NULL;
        if(isEmpty()) {
            front = temp;
        }//end if

        else
            back->next = temp;
        back = temp;
    }//end try
    catch (bad_alloc e)
    {
        throw string("Memory cannot be allocated.");
    }//end catch

}//end enqueue

template <typename T> void Queue<T>::dequeue()
{
    if(!isEmpty())
    {
        qNode *temp = front;
        if(front == back)
        {
            front = NULL;
            back = NULL;
        }
        else
        {
            front = front->next;
            temp->next = NULL;
            delete temp;
        }//end else
    }//end if
}//end dequeue

template <typename T> T Queue<T>::getFront() const
{
    if(!isEmpty())
    {
        return front->element;
    }//end if
}//end getFront



//end q.h

And CharQueueTemplate.h which contains the abstract base class CharQueue:

template <typename T>
class CharQueue
{
   public:
       virtual bool isEmpty() const = 0;
       virtual void enqueue(const T & newItem) = 0;
       virtual T dequeue(void) = 0;  // dequeue an item and return its value
       virtual T getFront(void) const = 0;  // return the value of the first item in the queue (but do not dequeue it)
};

Any input input anyone has on this would be greatly appreciated, since this error has me stumped right now.

Do this and see what happened:

template <typename T> class Queue : public CharQueue <T>

When one class inherits from another, the object of the derived class has contained within it an object of the base class. In order for the inheritance to work with templates, you have to provide a template argument for the compiler to use when generating the base object.

Thus the need to write it as:

template <typename T> class Derived : public Base<T>

The "<T>" is the template argument for the Base template.

Sorry about taking so long to reply, but I made the recommended changes and its all working great so far. Thanks.

Sorry about taking so long to reply, but I made the recommended changes and its all working great so far. Thanks.

Not a big deal. It happens more often than not.

Glad you got it sorted out.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.