[英]C: variable has initializer but incomplete type
試圖繞過舊的C語言。 當前在結構上並出現此錯誤:
"variable 'item1' has initializer but incomplete type"
這是我的代碼:
typedef struct
{
int id;
char name[20];
float rate;
int quantity;
} item;
void structsTest(void);
int main()
{
structsTest();
system("PAUSE");
return 0;
}
void structsTest(void)
{
struct item item1 = { 1, "Item 1", 345.99, 3 };
struct item item2 = { 2, "Item 2", 35.99, 12 };
struct item item3 = { 3, "Item 3", 5.99, 7 };
float total = (item1.quantity * item1.rate) + (item2.quantity * item2.rate) + (item3.quantity * item3.rate);
printf("%f", total);
}
我猜可能是結構定義在錯誤的位置,所以我將其移到文件的頂部並重新編譯,但是仍然遇到相同的錯誤。 我的錯在哪里
刪除item
之前的struct
,您已經輸入了它。
typedef struct { ... } item
創建一個未命名的struct
類型,然后將typedef
命名為name item
。 因此,沒有struct item
-只有item
和未命名的struct
類型。
使用struct item { ... }
,或將所有struct item item1 = { ... }
更改為item item1 = { ... }
。 您選擇哪一個取決於您的偏好。
問題是
typedef struct { /* ... */ } item;
不聲明類型名稱struct item
,僅聲明item
。 如果要同時使用兩個名稱,請使用
typedef struct item { /* ... */ } item;
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.