【问题标题】:Incomplete type not allowed不允许不完整的类型
【发布时间】:2014-04-22 18:12:06
【问题描述】:

我正在尝试通过将内存分配给指向队列的指针来创建队列,但是当我在 size 槽中写入结构的名称时出现此错误。我真的不知道怎么解释,所以代码:

结构,在 queue.c 中定义:

struct queue {
    Node front;
    Node back;
    int num;
    int (*e_func)(Elem,Elem);
};
typedef struct node* Node;
struct node {
    Elem e;
    Node next;
    Node prev;
};

队列.h:

#ifndef QUEUE_H
#define QUEUE_H
#include <malloc.h>
#include <stdlib.h>
typedef struct queue* Queue;
typedef void* Elem;
Queue CreateQ(int (*equals)(Elem,Elem));
void Enqueue(Queue Q, Elem e);
Elem Dequeue();
Elem front(Queue Q);
Elem back(Queue Q);
int size(Queue Q);
int isIn(Queue Q, Elem e);
#endif

队列的使用,在funcs.c中:

Queue Q = (Queue)malloc(sizeof(struct queue));

将鼠标悬停在 sizeof() 参数中的 struct 上时会显示错误。我到底做错了什么?我记得过去做过类似的事情,而且效果很好。我相信这可能与节点中的 void 指针有关,但是我该如何解决呢?

【问题讨论】:

标签: c struct incomplete-type


【解决方案1】:

首先定义 typedef 'Node',以便它可用于下一个结构定义。 然而,现在在 struct 'node' 中,'next' 和 'prev' 元素不能是 'Node' 类型,因为它是在完成 typedef 之后才定义的。然而,'struct node *' 可用于定义'next' 和'prev',因为'struct node' 已充分定义。

typedef struct node {
    Elem e;
    struct node *next;
    struct node *prev;
    } *Node;

稍后,你会发现你可以引用'next'和'prev',就像它们被定义为'Node'类型一样;因为 type 'Node' 和 type 'struct node *' (对编译器)是等价的。

struct queue {
    Node front;
    Node back;
    int num;
    int (*e_func)(Elem,Elem);
    };

【讨论】:

  • 张贴代码没有解释,虽然它可以解决问题,但不是很有帮助。您能否简要说明您所做的更改以及它们如何解决问题?
猜你喜欢
  • 1970-01-01
  • 2011-08-12
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2013-10-26
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多