gpt4 book ai didi

c++ - 队列类中非void delete成员函数

转载 作者:太空宇宙 更新时间:2023-11-04 13:07:07 25 4
gpt4 key购买 nike

这是 C++ 中队列类的实现。我想不通的是我想要一个函数删除,我在其中返回已删除的元素,但出现了这些错误:“删除”之前预期的不合格 ID预期的 ;在成员声明结束时

当我删除 delete 函数时,一切正常,但当它存在时就不行了。我正在尝试使用 bfs 在未加权的无向图中找到最短路径,我需要队列,特别是返回已删除顶点的队列。

class Queue
{
public:
Queue(int maxQueueSize):MaxSize(maxQueueSize)
{
queue=new int[MaxSize];
front=rear=-1;
}
bool isFull()
{
if(rear==MaxSize-1)
return true;
else return false;
}
bool isEmpty()
{
if(front==rear)
return true;
else return false;
}
void add(const int& x)
{
if(isFull())
return;
else
queue[++rear]=x;
}
int* delete()
{
int& x;
if(isEmpty())
return 0;
else
{
x=queue[++front];
}
return x;
}
private:
int front,rear;
int* queue;
int MaxSize;
};

最佳答案

'delete'是C++的保留关键字。
查看保留关键字列表 here .

你最好称它为“删除”或类似的名称。
更好的是,只需使用 std::queue<>,它已经为您精心编写和测试。

关于c++ - 队列类中非void delete成员函数,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/41696192/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com