错误:'node'未声明(首次在此函数中使用)

时间:2013-11-26 02:59:25

标签: c struct malloc

我在编译时遇到此错误

queue.c: In function ‘enqueue’:
queue.c:25:10: error: ‘node’ undeclared (first use in this function)
queue.c:25:10: note: each undeclared identifier is reported only once for each function it appears in

为什么会出现这种情况?我不明白如何定义变量节点。

这是我的入队代码:

   void enqueue(Queue* queue, int value){
      QNode* node =(QNode *)malloc(sizeof(struct QNode));
      node->data = value;
      if(queue->size == 0){
        q->front = node;
        q->rear = node;
      }else{
        queue->rear->next = node;
        queue->rear = node;
        queue->size++;
      }
}

在文件的开头,我用这种方式定义QNode和Queue:

typedef struct QNode;
struct QNode{
  struct QNode* next;
  int data;
} QNode;

typedef struct Queue{
  int size;
  struct QNode* front;
  struct QNode* rear;
} Queue;

1 个答案:

答案 0 :(得分:1)

虽然技术上不是错误:

typedef struct QNode;
struct QNode{
  ...

最好写成:

typedef struct QNode {
    ...

您还有:

if(queue->size == 0){
        q->front = node;
        q->rear = node;

q定义在哪里?

也许你的意思是:

if(queue->size == 0){
        queue->front = node;
        queue->rear = node;