2017-01-30 45 views
0

嗨,我遇到了取消引用指向不完整類型的指針。這很奇怪。我需要幫助 Graph.h //無向加權圖算法接口這裏有一個非常奇怪的錯誤:取消引用指向不完整類型的指針

typedef int Vertex; 

typedef struct { 
    Vertex v; 
    Vertex w; 
    int weight; 
} Edge; 

Edge mkEdge(Vertex, Vertex, int); 

typedef struct graphRep *Graph; 

Graph newGraph(int nV); 

void insertE(Graph g, Edge e); 

Graph.c //實施後的一部分

#include <stdio.h> 
#include <stdlib.h> 
#include <assert.h> 
#include <string.h> 
#include "Graph.h" 

struct graphRep { 
    int V; 
    int E; 
    int **edges; 
} 


int validV(Graph g, Vertex v); 

int validV(Graph g, Vertex v){ 
    return (v >= 0 && v < g->V); 
} 
// Create an edge from v to w 
Edge mkEdge(Vertex v, Vertex w,int weight) { 
     assert(v >= 0 && w >= 0 ); 
     Edge e = {v,w,weight}; 
     return e; 
} 
Graph newGraph(int nV) { 

    assert(nV >= 0); 
    int i,j; 
    Graph g = malloc(sizeof(struct graphRep)); 
    assert(g!=NULL); 
    if(nV==0){ 
     g->edges = NULL; 
    } else { 
     g->edges = malloc(nV*sizeof(int *)); 
    } 
    for(i = 0; i < nV;i++){ 
     g->edges[i] = malloc(nV * sizeof(int)); 
     assert(g->edges[i] != NULL); 
     for(j = 0; j < nV; j++){ 
     g->edges[i][j] = 0; 
     } 
    } 
    g->V = nV; 
    g->E = 0; 
    return g; 
} 

testGraph.c //測試

的一部分
#include <stdio.h> 
#include <stdlib.h> 
#include <assert.h> 
#include <string.h> 
#include "Graph.h" 
int main(void){ 
     printf("boundary test for newGraph\n"); 
     Graph g = newGraph(0); 
     assert(g!=NULL); 
     assert(g->V == 0 && g->E ==0 && g->edges == NULL); 
     printf("test passed!\n"); 
     free(g); 
     return 0; 
} 

我很困惑,因爲我做了
typedef結構graphRep *圖表 這意味着它是一個帶指針的結構。 但還是得到了這些錯誤

wagner % gcc -Wall -Werror Graph.c testGraph.c 
In file included from testGraph.c:3:0: 
testGraph.c: In function 'main': 
testGraph.c:30:12: error: dereferencing pointer to incomplete type 
    assert(g->V == 0 && g->E ==0 && g->edges == NULL); 
      ^
testGraph.c:30:25: error: dereferencing pointer to incomplete type 
    assert(g->V == 0 && g->E ==0 && g->edges == NULL); 
         ^
testGraph.c:30:37: error: dereferencing pointer to incomplete type 
    assert(g->V == 0 && g->E ==0 && g->edges == NULL); 
            ^

有人幫助我T T [

+1

struct graphRep對於「testGraph.c」文件是未知的。如果你想隱藏graphRep的細節,那麼你可以使用不透明的指針概念。 – rajesh6115

回答

2

testGraph.c看不到結構定義爲Graph.c

移動struct graphRepGraph.h接口文件。

+0

但是對於一個好的ADT,它應該被隱藏起來。我做了一些圖輔導,其中導師不允許改變界面,他們都做得很好 –

+1

你可以這樣做,但是你不能取消引用那個'struct'的指針。因此,對該結構的所有訪問都必須由'Graph.c'文件中的'get/set'函數實現,並使用'.h'接口文件提供給其他人。 – LPs

相關問題