【发布时间】:2021-12-30 22:38:39
【问题描述】:
我刚刚开始使用 c++ 学习队列。我需要添加 10 个名称,显示它们,然后删除它们,然后重新显示名称,我尝试这样做,但我收到错误“操作员 = 不明确” .请帮助。 我不知道我哪里出错了。我知道我们应该添加 10 个名称,然后将它们传递给 insert() 那么我们应该删除队列中的字符串 然后在显示后它应该返回全 0
'''
template<class t>
class Queue
{
private:
int front;
int rear;
t arr[10];
public:
Queue()
{
front = -1;
rear = -1;
for (int i = 0; i < 10; i++)
{
arr[i] = 0;
}
}
int isEmpty()
{
if (front == -1 && rear == -1)
return 1;
else
return 0;
}
int isFull()
{
if (rear == 9)
return 1;
else
return 0;
}
void insertion(t value)
{
if (isFull()==1)
{
cout << "Cant be inserted ,Queue full" << endl;
}
else if (isEmpty()==1)
{
front = 0;
rear = 0;
arr[rear] = value;
}
else
{
rear++;
arr[rear] = value;
}
}
t deletion()
{
t x ;
if (isEmpty()==1)
{
cout << "Cant be deleted ,Queue is Empty" << endl;
return x;
}
else if (rear == front)
{
x = arr[rear];
rear = -1;
front = -1;
return x;
}
else
{
cout << "front value: " << front << endl;
x = arr[front];
arr[front] = 0;
front++;
return x;
}
}
int count()
{
return (rear - front + 1);
}
void display()
{
cout << "All values in the Queue are - " << endl;
for (int i = 0; i < 10; i++)
{
cout << arr[i] << " ";
}
}
};
int main() {
Queue < string> q1;
int value, option;
string name;
cout << "Enter 10 names";
for (int i = 0; i < 10; i++)
{
cin >> name;
q1.insertion(name);
}
for (int i = 0; i < 10; i++)
{
q1.deletion();
}
for (int i = 0; i < 10; i++)
{
q1.insertion(name);
}
return 0;
}'''
【问题讨论】: