2014-05-22 61 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對它們進行初始化很繁瑣。

相關問題