简体   繁体   中英

C++ Queue Implementation Error

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!

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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM