10

そのため、突然、コンパイラはこれに直面して吐き出すことにしました:「フィールドの顧客には不完全な型があります」。

関連するコードのスニペットは次のとおりです。

customer.c

#include <stdlib.h>
#include <string.h>

#include "customer.h"

struct CustomerStruct;
typedef struct CustomerStruct
{
    char id[8];
    char name[30];
    char surname[30];
    char address[100];
} Customer ;

/* Functions that deal with this struct here */

顧客.h

customer.h のヘッダー ファイル

#include <stdlib.h>
#include <string.h>

#ifndef CUSTOMER_H
#define CUSTOMER_H

    typedef struct CustomerStruct Customer;

    /* Function prototypes here */

#endif

これが私の問題です:

customer_list.c

#include <stdlib.h>
#include <string.h>

#include "customer.h"
#include "customer_list.h"

#include "..\utils\utils.h"


struct CustomerNodeStruct;
typedef struct CustomerNodeStruct
{
    Customer customer; /* Error Here*/
    struct CustomerNodeStruct *next;
}CustomerNode;



struct CustomerListStruct;
typedef struct CustomerListStruct
{
    CustomerNode *first;
    CustomerNode *last;
}CustomerList;

/* Functions that deal with the CustomerList struct here */

このソース ファイルにはヘッダー ファイル customer_list.h がありますが、関連性はないと思います。

私の問題

customer_list.c の comment のある行で/* Error Here */、コンパイラは次のように文句を言いますfield customer has incomplete type.

私は一日中この問題をグーグルで調べていましたが、今は眼球を取り出してイチゴとブレンドするところです.

このエラーの原因は何ですか?

前もって感謝します :)

[追記: 言い忘れがありましたら、お知らせください。あなたが言うかもしれないように、それは私にとってストレスの多い日でした]

4

4 に答える 4

15

構造体宣言をヘッダーに移動します。

customer.h
typedef struct CustomerStruct
{
...
}
于 2012-11-29T20:53:25.427 に答える
8

C では、コンパイラは、直接参照されるオブジェクトのサイズを把握できる必要があります。sizeof(CustomerNode)を計算できる唯一の方法は、コンパイラが を構築しているときに Customer の定義を利用できるようにすることですcustomer_list.c

解決策は、構造体の定義を から に移動するcustomer.cことcustomer.hです。

于 2012-11-29T20:55:30.407 に答える
3

あなたが持っているのは、Customerインスタンス化しようとしている構造の前方宣言です。これは実際には許可されていません。これは、コンパイラが定義を確認しない限り、構造のレイアウトを認識できないためです。そのため、定義をソース ファイルからヘッダーに移動する必要があります。

于 2012-11-29T20:54:17.840 に答える