如何调试我的C++队列代码?
我试图编写一个实现队列的代码,但是在第五次输入之后,出现了一个错误。我能做些什么 这是我实现队列的代码:如何调试我的C++队列代码?,c++,C++,我试图编写一个实现队列的代码,但是在第五次输入之后,出现了一个错误。我能做些什么 这是我实现队列的代码: #include<iostream> #include<queue> #include<string> using namespace std; int capacity = 10, front=0,back = 0; string x; string Q[10]; class LapyTopy{
#include<iostream>
#include<queue>
#include<string>
using namespace std;
int capacity = 10, front=0,back = 0;
string x;
string Q[10];
class LapyTopy{
public:
void enqueue(string);
void dequeue();
void show();
};
//function to add into queue
void LapyTopy :: enqueue(string x)
{
int newback = (back + 1) % capacity;
if(newback!= front){
Q[back] = x;
back = newback + 1;
}
else
cout << "***Queue is Full! " << endl;
}
void LapyTopy :: dequeue() //function to remove from a queue
{
if(front!= back) {
front = (front + 1) % capacity;
}
else
cout << "***Queue is empty! " << endl;
}
//function to display queue
void LapyTopy :: show()
{
for(int i = front; i != back; i = (i + 1) % capacity){
cout << Q[i] <<" ";
}
cout << endl;
}
int main() // Is the problem here?
{
LapyTopy j;
string brand[capacity];
cout << "Please enter a few brand names: " << endl;
for(int i = 0; i < capacity; i++){
cin >> brand[i];
x = brand[i];
j.enqueue(x);``
cout << "The queue so far: " << endl;
j.show();
}
j.dequeue();
return 0;
}
您认为问题出在哪里?将行back=newBack+1更改为back=newBack。您只需添加1两次