2014-05-22 31 views
0

当我编译我的文件,他们是5(api.c api.h datastruct.c datastruct.h和main.c)与MakeFile的问题是在datastruct.c和datastruct我有我的大学项目的麻烦。 h当编译此功能:我的struct typedef导致“解除引用指向不完整类型的指针?”有什么问题?

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 
+0

什么问题?请显示错误消息什么编译器输出。 –

+1

关于风格的评论:typedef'ing指针在我看来是一个很大的错误,因为在C中知道你正在处理的是非常重要的。我只是吮吸它并在我需要的地方输入'struct vertex_t *'。 –

+0

你也可以使用'calloc'来分配内存,'calloc'将内存设置为0.所以你不需要所有这些NULL赋值。 –

回答

2

你的问题是在这里:

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

它应该是:

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

我发现了它。

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

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

3

仔细阅读你写的:

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; 

无关的问题所在,并在用户昝山猫评论为说,calloc分配将零的所有成员的结构,因此使用NULL对它们进行初始化很繁琐。

相关问题