2012-03-07 113 views
1

私は同じエラーで約5つの異なる質問を読んだが、私はまだ私のコードの問題を見つけることができません。C - 不完全な型への参照を間接参照

typedef struct graph graph_t; 

の.h

main.cの

int main(int argc, char** argv) { 
    //graph_t * g = graph_create(128); //I commented this line out to make sure graph_create was not causing this. 
    graph_t * g; 
    g->cap; //This line gives that error. 
    return 1; 
} 

.C

struct graph { 
    int cap; 
    int size; 
}; 

ありがとう!

+0

いただきましエラー? –

+0

@GregBrown:彼は不完全な型エラーへの逆参照ポインタを取得しています。 –

答えて

2

構造体は別のソースファイルで定義されているので、これを行うことはできません。 typedefの全体のポイントは、あなたからデータを隠すことです。おそらくgraph_capgraph_sizeのような関数があり、あなたのためにデータを返すことができます。

これがコードの場合は、ヘッダーファイル内にstruct graphを定義して、このヘッダーを含むすべてのファイルに定義できるようにする必要があります。

+0

ありがとう、これは理にかなっています。また、私の質問にも答えた他の人に感謝します。 – user1255321

0

あなたが定義したものでなければなりません。 typedef行は、main()を持つファイルに含まれているヘッダーファイルに表示する必要があります。

それ以外の場合はうまく動作しました。

0

lala.c

#include "lala.h" 

int main(int argc, char** argv) { 
    //graph_t * g = graph_create(128); //I commented this line out to make sure graph_create was not causing this. 
    graph_t * g; 
    g->cap; //This line gives that error. 
    return 1; 
} 

lala.h

#ifndef LALA_H 
#define LALA_H 

struct graph { 
    int cap; 
    int size; 
}; 

typedef struct graph graph_t; 

#endif 

これはと問題なくコンパイル:コンパイラはを参照できるようにする必要がありmain.cをコンパイルした場合

gcc -Wall lala.c -o lala 
1

capという名前のメンバーが存在することがわかるように、struct graphの定義。構造体の定義を.cファイルから.hファイルに移動する必要があります。 をopaque data typeにする必要がある場合は、graph_tポインタを取り、フィールド値を返すアクセサ関数を作成することです。例えば、

graph.h

int get_cap(graph_t *g); 

graph.c

int get_cap(graph_t *g) { return g->cap; } 
関連する問題