【发布时间】:2013-07-17 05:05:45
【问题描述】:
正如标题所说,我有这个代码:
typedef struct Book{
int id;
char title[256];
char summary[2048];
int numberOfAuthors;
struct Author *authors;
};
typedef struct Author{
char firstName[56];
char lastName[56];
};
typedef struct Books{
struct Book *arr;
int numberOfBooks;
};
我从 gcc 得到这些错误:
bookstore.c:8:2: error: unknown type name ‘Author’
bookstore.c:9:1: warning: useless storage class specifier in empty declaration [enabled by default]
bookstore.c:15:1: warning: useless storage class specifier in empty declaration [enabled by default]
bookstore.c:21:2: error: unknown type name ‘Book’
bookstore.c:23:1: warning: useless storage class specifier in empty declaration [enabled by default]
如果我像这样更改 typedef,则不会出现警告和错误:
typedef struct{
char firstName[56];
char lastName[56];
} Author;
通过 C Programming Language, 2nd Edition 搜索并搜索了几个小时,我无法弄清楚为什么第一个实现不起作用。
【问题讨论】:
-
将
Author移到Book之前。另请注意,您的typedefs 是多余的 -
Author结构中的更改怎么可能同时删除error:unknown type name ‘Book’?请看一下here,它清楚地提到了 typdef 结构和仅定义结构之间的区别。