Question

I am trying to make a basic queue. My header file is as follows:

#ifndef Queue_h
#define Queue_h

/**
    A simple Queue data structure
*/
template <class T>
class Queue{
public:
Queue(int s){
    size = s > 0 && s < 1000 ? s : 10;
    top = -1;
    queuePtr = new T[size];
}

~Queue(){
    delete[] queuePtr;
}

bool isEmpty(){
    return top == -1;
}

bool isFull(){
    return top == size - 1;
}

void push(const T){
    if(!isFull()){
        queuePtr[++top] = T;
    }
}

T pop(){
    if(!isEmpty()){
        return queuePtr[top--];
    }
}

 private:
int size;
int top;
T* queuePtr;
};

#endif

I am getting the following error message

Queue.h: In member function 'void Queue<T>::push(T)':
Queue.h:30: error: expected primary-expression before ';' token

I am unsure why the expressoin shown is not considered a primary-expression. Any help or links would be appreciated Thanks in advance!

Was it helpful?

Solution

You're treating a type (T) as a variable. The code

void push(const T){
    if(!isFull()){
        queuePtr[++top] = T;
    }
}

should be

void push(const T& item){
    if(!isFull()){
        queuePtr[++top] = item;
    }
}

or something like that.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top