c - 错误: ‘node’ undeclared (first use in this function)

标签 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;

最佳答案

虽然从技术上来说并不是一个错误:

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;

关于c - 错误: ‘node’ undeclared (first use in this function),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/20207694/

相关文章:

C中检查输入是否为数字

c - 我们应该错误检查 C 中的每个调用吗?

c - 将数组指针传递给对数字进行排序的函数

pointers - 戈朗 : Assigning a value to struct member that is a pointer

c# - 在 C# 中将结构转换为 byte[]

c - 结构数组上的 free()

c - C 中随机不使用 rand() 函数

c - 结构行为

c - 如何在 C 中使用结构体和指针的指针中的索引

c - 如何覆盖标准 libc 函数?