web-dev-qa-db-ja.com

CTypedef-不完全な型

したがって、突然、コンパイラはこれを正面から吐き出すことにしました:「フィールドの顧客は不完全な型を持っています」。

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

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 */

customer.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のコメント/* Error Here */の行で、コンパイラはfield customer has incomplete type.について文句を言います。

私は一日中この問題をグーグルで調べていました、そして今、私の眼球を引き出して、それらをイチゴと混ぜ合わせているところです。

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

前もって感謝します :)

[追記何か言及するのを忘れた場合は、私に知らせてください。あなたが言うかもしれないように、それは私にとってストレスの多い日でした]

7
Zyyk Savvins

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

customer.h
typedef struct CustomerStruct
{
...
}
12
cnicutar

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

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

6
user295691

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

3
user405725

のようなもののようです

typedef struct foo bar;

ヘッダーに定義がないと機能しません。しかし、

typedef struct foo *baz;

ヘッダーでbaz->xxxを使用する必要がない限り、機能します。

1
David Fernandez