试着绕过旧的C语言.目前在结构上并收到此错误:
"variable 'item1' has initializer but incomplete type"
Run Code Online (Sandbox Code Playgroud)
这是我的代码:
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);
}
Run Code Online (Sandbox Code Playgroud)
我猜测结构定义可能位于错误的位置,所以我将它移动到文件的顶部并重新编译,但我仍然得到相同的错误.哪里是我的错?
Chr*_*utz 10
typedef struct { ... } item创建一个未命名的struct类型,然后创建typedef名称item.所以没有struct item- 只是item一个未命名的struct类型.
使用struct item { ... }或更改所有struct item item1 = { ... }s item item1 = { ... }.你做哪一个取决于你的偏好.
问题是
typedef struct { /* ... */ } item;
Run Code Online (Sandbox Code Playgroud)
struct item只声明类型名称item。如果要同时使用两个名称,请使用
typedef struct item { /* ... */ } item;
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
32487 次 |
| 最近记录: |