繁体   English   中英

我的struct typedef有什么问题导致“将指针解引用为不完整的类型?”

[英]What is wrong with my struct typedef causing “dereferencing pointer to incomplete type?”

我在使用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.hi中具有结构定义:

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;

我找到了。

您的问题出在您的typedef中。 在C中,typedef创建一个新的类型名称。 但是,结构名称不是类型名称。

因此,如果将typedef struct vertex_t *vertex更改为typedef vertex_t *vertex ,它将修复该错误消息。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM