c -我的结构类型定义导致“解引用指针指向不完整类型”是什么问题?



当我编译我的文件时,我的大学项目遇到了麻烦,它们是5 (api.c api.h datastruct.c datastruct.h和main.c)与MakeFile问题是在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'

但是vertex是什么呢?

typedef struct vertex_t *vertex; // A pointer to a 'struct vertex_t'

那么struct vertex_t是什么?好吧,它不存在。您定义了以下内容:

typedef struct _vertex_t {
    ...
} vertex_t;

有两个定义:

  1. struct _vertex_t
  2. 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初始化它们是多余的。

你的问题在这里:

typedef struct vertex_t *vertex;
typedef struct edge_t *alduin;

应该是:

typedef struct _vertex_t *vertex;
typedef struct _edge_t *alduin;

我找到了

你的问题在于你的类型定义。在C语言中,typedef创建了一个新的类型名。但是,结构名不是类型名。

所以如果你把typedef struct vertex_t *vertex改成typedef vertex_t *vertex它会修复这个错误信息

最新更新