这里有一个非常奇怪的错误:取消引用指向不完整类型的指针

时间:2017-01-30 07:25:02

标签: c pointers dereference

嘿伙计们,我遇到了解除指向不完整类型的指针。这很奇怪。我需要帮助 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 struct graphRep * Graph      这意味着它是一个带指针的结构。 但仍然有这些错误

 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 个答案:

答案 0 :(得分:2)

testGraph.c无法看到Graph.c

中定义的结构

struct graphRep移动到Graph.h接口文件中。