2017-01-30 8 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 // 일부 내가 그것을 의미
형식 정의 구조체 graphRep * 그래프 을했기 때문에
#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; 
} 

나는 너무 혼란 스러워요 포인터가있는 구조체. 하지만 여전히 이러한 실수

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

"testGraph.c"파일에서 graphRep 구조체를 알 수 없습니다. graphRep의 세부 사항을 숨기려면 불투명 포인터 개념을 사용할 수 있습니다. – rajesh6115

답변

2

testGraph.c 구조체가 Graph.h 인터페이스 파일에 Graph.c

이동 struct graphRep로 정의 볼 수 없습니다.

+0

그러나 좋은 ADT의 경우 숨겨져 있어야합니다. 나는지도 교사가 인터페이스를 바꾸는 것을 허락하지 않았고 그들 모두가 잘 할 그래프 숙제 중 몇 가지를했습니다. –

+1

당신은 그렇게 할 수는 있지만,'struct'에 대한 포인터를 역 참조 할 수는 없습니다. 그래서 그 구조체에 대한 모든 접근은'Graph.c' 파일의'get/set' 함수에 의해 구현되어야하고'.h' 인터페이스 파일을 사용하여 다른 사람들이 이용할 수있게해야합니다. – LPs