1

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!

1 Answer 1

3

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.

Sign up to request clarification or add additional context in comments.

2 Comments

Oh man, that was so simple. Thank you very much, haha, I will be sure to give you accepted answer!
Happens to the best of us. It's always nice when the mistake makes sense.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.