当我在Turbo C ++编译器中运行此C ++代码时,它给出了以下错误:
#include<iostream.h>
#include<conio.h>
#define MAXSIZE 10
#include<stdlib.h>
class queue
{
int s[MAXSIZE],front,rear,i;
public:
queue(){front=-1;rear=-1;}
void insert(int val)
{
if(rear==MAXSIZE-1)
cout<<"queue is full";
else
if (front==-1)
{
front=0;
}
rear=rear+1;
cout<<"\n"<<" Enter the info" ;
cin>>s[val];
}
void del()
{
if(front==-1)
cout<<"queue is empty";
else
cout<<"item deleted"<<s[front];
if(front==rear)
front=rear=-1;
else
front=front+1;
}
void traverse ()
{
if(front==-1)
cout"queue is empty";
else
{
for(i=front;i<=rear;i++)
cout<<"\t"<<s[i];
{
}
};
void main()
{
linklist l1;
l1.insert(10);
l1.insert(20);
l1.insert(30);
l1.traverse();
l1.del() ;
l1.insert(40);
getch();
}
答案 0 :(得分:1)
这应该编译,但它不会按预期工作。我不打算尝试修复它,因为在继续之前,通过在文本中读取队列,逻辑太破坏并处理得更好。
我不能确定这是在Turbo C ++中编译的。我从90年代开始就没有使用Turbo C ++,即便如此,我只是用它来编译C代码。
解释嵌入在评论中,以便将所有内容保存在一个块中。
#include<iostream> // don't need the .h
#include<conio.h>
#define MAXSIZE 10
#include<stdlib.h>
// need to specify namespaces. Don't pull in the whole namespace unless you
// know exactly what you are doing. JUst use the parts you need or explicitly
// state with every use eg. std::cout << "queue is full";
using std::cout;
using std::cin;
using std::endl;
class queue
{
int s[MAXSIZE], front, rear; // no need to define i here. Only used in
// the traverse method and has no need for
// persistence
public:
queue()
{
front = -1;
rear = -1;
}
void insert(int val)
{ // this function almost certainly does not work logically
// a queue adds to one end and takes from the other. This allows the
// caller to put an element anywhere in the queue, destroying
// whatever value was in that slot.
if (rear == MAXSIZE - 1)
{ // always use all of the braces while learning. You can take the
// training wheels off when you know how to do it safely.
cout << "queue is full";
}
else if (front == -1)
{
front = 0;
}
rear = rear + 1;
cout << "\n" << " Enter the info";
// using \n in place of std::endl may have unexpected consequences
// with respect to flushing. std:: endl means end the line and write the
// output. In this case,display on the console. \n means end the line and
// write when you feel like it. Result is the user prompt may not be
// written before it stops for user input on the next line.
cin >> s[val];
}
void del()
{ // this function's logic is also broken.
if (front == -1)
{
cout << "queue is empty";
}
else
{
cout << "item deleted" << s[front];
}
if (front == rear)
{
front = rear = -1;
}
else
{
front = front + 1;
}
}
void traverse()
{
if (front == -1)
{
cout << "queue is empty"; // missed the <<. This broke the for loop
// because the braces weren't used and
// the compiler choked rather than giving
// a reasonable error message.
}
else
{
for (int i = front; i <= rear; i++) // defined the i removed earlier
{
cout << "\t" << s[i];
}
// braces here were completely messed up.
}
}
};
void main()
{
queue l1; // was linklist rather than queue
l1.insert(10);
l1.insert(20);
l1.insert(30);
l1.traverse();
l1.del();
l1.insert(40);
getch();
}