队列结构给出奇怪的错误

发布于 2024-12-15 18:12:12 字数 1918 浏览 1 评论 0原文

这是队列实现,它给了我非常不清楚的错误

#include<iostream>
#include "bool.h"
#include "item.h"
#include "queue.h"

using namespace std;

void init_queue(queue *q){
    q->first=0;
    q->last=queuesize-1;
    q->count=0;

}
       void enqueue (queue *q,item_type x){
           if(q->count>=queuesize)
               cout<<" queue everflow occurs during enqueue "<<endl;
           else
           {
               q->last=(q->last+1)%queuesize;
               q->m[q->last]=x;
               q->count=q->count+1;

           }




       }

             int dequeue (queue *q)
             {
                 item_type x;
                 if(q->count<=0) cout<<"empthy queue "<<endl;
                 else
                 {
                     x=q->m[q->first];
                     q->first=(q->first+1)%queuesize;
                     q->count=q->count-1;
                 }
                 return (x);




                 }



             item_type headq(queue *q)
             {
                 return(q->m[q->first]);
             }
             int empthy(queue *q){


                 if (q->count<=0) return (TRUE);
                 else return (FALSE);
             }

 void print_queue(queue *q){
     int i;
     i=q->first;
     while(i!=q->last)
     {
         cout<<q->m[i];
         i=(i+1)%queuesize;
     }
     cout<<q->m[i]<<"  ";

 }
 int main(){
     queue *q;
     init_queue(q);
 int a;
 while(cin>>a){
     enqueue(q,a);




 }
 print_queue(q);


    return 0;
}

,另请参阅队列头文件

#define  queuesize 1000
#include "item.h"

typedef struct
{
int  m[queuesize+1];
  int first;
  int last;
  int count;
  }queue;

错误是(实际上)它不是错误,只是在我编译时发出警告,但是当我运行时它说局部变量 q 未初始化,那么哪个变量 q?结构的名称,所以当我运行 init_queue 方法时它应该初始化是吗?

here is queue implementation which gives me very unclear error

#include<iostream>
#include "bool.h"
#include "item.h"
#include "queue.h"

using namespace std;

void init_queue(queue *q){
    q->first=0;
    q->last=queuesize-1;
    q->count=0;

}
       void enqueue (queue *q,item_type x){
           if(q->count>=queuesize)
               cout<<" queue everflow occurs during enqueue "<<endl;
           else
           {
               q->last=(q->last+1)%queuesize;
               q->m[q->last]=x;
               q->count=q->count+1;

           }




       }

             int dequeue (queue *q)
             {
                 item_type x;
                 if(q->count<=0) cout<<"empthy queue "<<endl;
                 else
                 {
                     x=q->m[q->first];
                     q->first=(q->first+1)%queuesize;
                     q->count=q->count-1;
                 }
                 return (x);




                 }



             item_type headq(queue *q)
             {
                 return(q->m[q->first]);
             }
             int empthy(queue *q){


                 if (q->count<=0) return (TRUE);
                 else return (FALSE);
             }

 void print_queue(queue *q){
     int i;
     i=q->first;
     while(i!=q->last)
     {
         cout<<q->m[i];
         i=(i+1)%queuesize;
     }
     cout<<q->m[i]<<"  ";

 }
 int main(){
     queue *q;
     init_queue(q);
 int a;
 while(cin>>a){
     enqueue(q,a);




 }
 print_queue(q);


    return 0;
}

see also please queue header file

#define  queuesize 1000
#include "item.h"

typedef struct
{
int  m[queuesize+1];
  int first;
  int last;
  int count;
  }queue;

error is that (actualy) it is not error just warning when i compile,but when i run it says local variable q uninitialized,so which variable q?it is name of structure,so when i run init_queue method it should initialize yes?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(6

左岸枫 2024-12-22 18:12:12

您必须实例化队列对象。

int main(){
 queue *q = new queue;
 init_queue(q);

使用 q 作为堆栈变量并传递地址。

int main(){
 queue q;
 init_queue(&q);

You must instantiate the queue object.

int main(){
 queue *q = new queue;
 init_queue(q);

or

use q as a stack variable and pass the address.

int main(){
 queue q;
 init_queue(&q);
呆° 2024-12-22 18:12:12

问题出在这两行代码上。

 queue *q;
 init_queue(q);

init_queue 中,您假设 q 通过使用 -> 运算符取消引用而为其分配了一些内存。但它只是一个指向内存中随机地址的指针(因为它尚未初始化)。

q->first=0;
q->last=queuesize-1;
q->count=0;

要解决此问题,您应该添加一些内容,

queue *q = new queue();

但是,由于您使用的是 C++,因此可能值得构建一个类来封装构造函数中的内存分配并在析构函数中释放内存。

更好的是遵循 rageshctech 的建议,完全避免使用手动内存分配,而只使用堆栈上分配的内容(例如,没有指针)。

The problem is in these two lines of code.

 queue *q;
 init_queue(q);

Inside init_queue you are assuming the q has some memory allocated to it by dereferencing it using the -> operator. But it is just a pointer to a random address in memory (since it hasn't been initialized).

q->first=0;
q->last=queuesize-1;
q->count=0;

To fix this you should add something along the lines of

queue *q = new queue();

However, since you are in C++ it might be worth building a class to encapsulate the memory allocation in the constructor and free the memory in the destructor.

Even better would be to follow rageshctech's suggestion and avoid using manual memory allocation at all and just use something allocated on the stack (e.g. without a pointer).

殊姿 2024-12-22 18:12:12
 queue *q;      
init_queue(q); 

您没有为 q 分配内存。

队列 *q = 新队列;

您也可以在堆栈上分配队列:(推荐!)

queue q;
   init_queue(&q); 
 queue *q;      
init_queue(q); 

You are not allocating the memory for q.

queue *q = new queue;

You can allocate the queue on stack as well: ( recommended !)

queue q;
   init_queue(&q); 
十年九夏 2024-12-22 18:12:12

这是结构的名称

不,q 是一个指向queue 对象的指针。在 main 中,您还没有为 q 分配任何值,甚至还没有创建它可以指向的 queue 对象。所以 q 未初始化。

你应该这样做:

queue q;          // an instance of queue
queue *qptr = &q; // a pointer to that instance
// now use qptr (or &q) in place of q in the rest of the main function.

it is name of structure

No, q is a pointer-to-queue-object. In main, you haven't assigned any value to q, and you haven't even created a queue object that it could point to. So q is uninitialized.

You should do this:

queue q;          // an instance of queue
queue *qptr = &q; // a pointer to that instance
// now use qptr (or &q) in place of q in the rest of the main function.
抽个烟儿 2024-12-22 18:12:12

我猜警告是关于 main 中的变量 q 的?这是因为你将它定义为指针,但没有分配它。要么将其定义为非指针,并在所有函数调用中使用 &q,要么为其分配内存:

queue *q = new queue;

mainreturn 0 之前代码>释放内存:

delete q;

I guess the warning is about the variable q in main? It's because you define it as a pointer, but do not allocate it. Either define it as not a pointer, and use &q in all function calls, or allocate memory for it:

queue *q = new queue;

And before the return 0 in main free the memory:

delete q;
铁憨憨 2024-12-22 18:12:12

您从未为队列分配内存。

使用非默认构造函数创建一个类并使用它来代替指针。

指针是 C 语言的东西。你正在用 C++ 编码:P

You never allocated memory for the queue.

Instead of pointers, make a class with a non-default constructor and use that instead.

Pointers are a C thing. You're coding in C++ :P

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文