队列结构给出奇怪的错误
这是队列实现,它给了我非常不清楚的错误
#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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(6)
您必须实例化队列对象。
或
使用 q 作为堆栈变量并传递地址。
You must instantiate the queue object.
or
use q as a stack variable and pass the address.
问题出在这两行代码上。
在
init_queue
中,您假设q
通过使用->
运算符取消引用而为其分配了一些内存。但它只是一个指向内存中随机地址的指针(因为它尚未初始化)。要解决此问题,您应该添加一些内容,
但是,由于您使用的是 C++,因此可能值得构建一个类来封装构造函数中的内存分配并在析构函数中释放内存。
更好的是遵循 rageshctech 的建议,完全避免使用手动内存分配,而只使用堆栈上分配的内容(例如,没有指针)。
The problem is in these two lines of code.
Inside
init_queue
you are assuming theq
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).To fix this you should add something along the lines of
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).
您没有为 q 分配内存。
队列 *q = 新队列;
您也可以在堆栈上分配队列:(推荐!)
You are not allocating the memory for q.
queue *q = new queue;
You can allocate the queue on stack as well: ( recommended !)
不,
q
是一个指向queue
对象的指针。在main
中,您还没有为q
分配任何值,甚至还没有创建它可以指向的queue
对象。所以q
未初始化。你应该这样做:
No,
q
is a pointer-to-queue
-object. Inmain
, you haven't assigned any value toq
, and you haven't even created aqueue
object that it could point to. Soq
is uninitialized.You should do this:
我猜警告是关于
main
中的变量q
的?这是因为你将它定义为指针,但没有分配它。要么将其定义为非指针,并在所有函数调用中使用&q
,要么为其分配内存:在
mainreturn 0
之前代码>释放内存:I guess the warning is about the variable
q
inmain
? 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:And before the
return 0
inmain
free the memory:您从未为队列分配内存。
使用非默认构造函数创建一个类并使用它来代替指针。
指针是 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