【发布时间】:2019-10-14 04:20:02
【问题描述】:
我正在尝试实现一个循环队列。
我已经在头文件中声明了队列的大小,并且我通过构造函数使用大小变量启动了我的队列。
这里是 queue.h 和 queue.cpp 文件。
class Queue
{
public:
int size;
int front, rear;
int A[];
Queue(int size);
bool isEmpty();
void enqueue(int n);
int dequeue();
int Peek();
void Display();
int sizeQ();
};
这里是 queue.cpp
Queue::Queue(int size)
{
int A[size];
front = rear = -1;
}
bool Queue::isEmpty(){
if((front == -1) && (rear == -1))
return true;
else
return false;
}
void Queue::Display(){
if(isEmpty()){
cout << "Its empty! Nothing to display"<<endl;
}else{
for(int i=0; i<sizeQ(); i++){
cout << A[i] << endl;
}
}
cout <<endl;
}
这是我的主要内容
int main()
{
Queue q1(10);
q1.enqueue(20);
q1.Display();
return 0;
}
问题:尽管我在 main 中使用 size 创建了对象,但显示函数内部的循环看不到 size 变量。当我调试程序时,我看到大小为 0,因此 for 循环永远不会启动。
我尝试了什么
int Queue::sizeQ(){
return size;
}
我尝试通过方法返回大小;但是,没有运气。我应该怎么做才能访问 size 变量?
【问题讨论】:
标签: c++ oop member-functions