简体   繁体   English

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

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

Im having trouble with my project of the university when i compile my files they are 5 (api.c api.h datastruct.c datastruct.h and main.c) with MakeFile the problem is in the datastruct.c and datastruct.h when compiles this functions: 我在使用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);   
}

and in the file datastruct.hi have the structure definition: 在文件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;

i cant see the problem datastruct.h is included in datastruct.c!!! 我看不到问题datastruct.h包含在datastruct.c中!!! The error on compiler is: 编译器上的错误是:

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

Read carefully what you wrote: 仔细阅读您写的内容:

vertex new_vertex = NULL; // Declare an element of type 'vertex'

But what is vertex ? 但是什么是vertex

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

So what is a struct vertex_t ? 那么什么是struct vertex_t呢? Well, it doesn't exist. 好吧,它不存在。 You defined the following: 您定义了以下内容:

typedef struct _vertex_t {
    ...
} vertex_t;

That's two definitions: 这是两个定义:

  1. struct _vertex_t
  2. vertex_t

No such thing as struct vertex_t (the reasoning is similar for edge ). 没有诸如struct vertex_t这样的东西(关于edge的推理是相似的)。 Change your typedefs to either: 将您的typedef更改为:

typedef vertex_t *vertex;
typedef edge_t *edge;

Or: 要么:

typedef struct _vertex_t *vertex;
typedef struct _edge_t *edge;

Unrelated to your problem and as said in the comments by user Zan Lynx, allocating with calloc will zero all the members of your struct, therefore initializing them with NULL is superflous. 与您的问题无关,正如用户Zan Lynx在评论中所说,用calloc分配会将结构的所有成员都清零,因此用NULL初始化它们是多余的。

Your problem is here: 您的问题在这里:

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

it should be: 它应该是:

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

I found it. 我找到了。

Your problem is in your typedef. 您的问题出在您的typedef中。 In C typedef creates a new type name. 在C中,typedef创建一个新的类型名称。 Struct names however, are not type names. 但是,结构名称不是类型名称。

So if you change typedef struct vertex_t *vertex into typedef vertex_t *vertex it will fix that error message. 因此,如果将typedef struct vertex_t *vertex更改为typedef vertex_t *vertex ,它将修复该错误消息。

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

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