当我用 MakeFile 编译我的文件时,我的大学项目遇到了问题,它们是 5(api.c api.h datastruct.c datastruct.h 和 main.c),问题出在 datastruct.c 和 datastruct .h 编译这个函数时:
vertex new_vertex() {
/*This functions allocate memorie for the new struct vertex wich save
the value of the vertex X from the edge, caller should free this memorie*/
vertex new_vertex = NULL;
new_vertex = calloc(1, sizeof(vertex_t));
new_vertex->back = NULL;
new_vertex->forw = NULL;
new_vertex->nextvert = NULL;
return(new_vertex);
}
在文件 datastruct.h 中我有结构定义:
typedef struct vertex_t *vertex;
typedef struct edge_t *alduin;
typedef struct _edge_t{
vertex vecino; //Puntero al vertice que forma el lado
u64 capacidad; //Capacidad del lado
u64 flujo; //Flujo del lado
alduin nextald; //Puntero al siguiente lado
}edge_t;
typedef struct _vertex_t{
u64 verx; //first vertex of the edge
alduin back; //Edges stored backwawrd
alduin forw; //Edges stored forward
vertex nextvert;
}vertex_t;
我看不到问题 datastruct.h 包含在 datastruct.c 中!!! 编译器的错误是:
gcc -Wall -Werror -Wextra -std=c99 -c -o datastruct.o datastruct.c
datastruct.c: In function ‘new_vertex’:
datastruct.c:10:15: error: dereferencing pointer to incomplete type
datastruct.c:11:15: error: dereferencing pointer to incomplete type
datastruct.c:12:15: error: dereferencing pointer to incomplete type
最佳答案
仔细阅读你写的内容:
vertex new_vertex = NULL; // Declare an element of type 'vertex'
但是顶点
是什么?
typedef struct vertex_t *vertex; // A pointer to a 'struct vertex_t'
那么什么是struct vertex_t
?好吧,它不存在。您定义了以下内容:
typedef struct _vertex_t {
...
} vertex_t;
这是两个定义:
结构_vertex_t
vertex_t
没有 struct vertex_t
这样的东西(edge
的道理类似)。将您的 typedef 更改为:
typedef vertex_t *vertex;
typedef edge_t *edge;
或者:
typedef struct _vertex_t *vertex;
typedef struct _edge_t *edge;
与您的问题无关,正如用户 Zan Lynx 在评论中所说,使用 calloc
分配会将结构的所有成员归零,因此使用 NULL
初始化它们是多余的。
关于c - 我的 struct typedef 有什么问题导致 "dereferencing pointer to incomplete type?",我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/23796189/