我是C语言的头文件新手。
我想做的是使用makefile一起编译我的代码文件。我有2个头文件,每个头2个c文件,还有1个main.c文件。
我有main.c,这是我的主要功能,它具有“ #include“ dict2.h””。 dict1和dict2标头有些相同。区别在于dict2具有附加的链表功能。
-bash-4.1$ make dict1
gcc -Wall -c main.c -o main.o -g
In file included from main.c:6:
dict2.h:1: warning: useless storage class specifier in empty declaration
dict2.h:8: warning: useless storage class specifier in empty declaration
dict2.h:21: error: expected declaration specifiers or '...' before 'record_t'
dict2.h:24: error: expected declaration specifiers or '...' before 'record_t'
dict2.h:42: error: expected '=', ',', ';', 'asm' or '__attribute__' before '*' token
dict2.h:45: error: expected '=', ',', ';', 'asm' or '__attribute__' before '*' token
dict2.h:48: error: expected ')' before '*' token
make: *** [main.o] Error 1
我的dict2.h功能看起来像这样:
typedef struct record_t;
typedef struct node_list node_list_t;
struct node_list;
typedef struct list_t;
typedef struct node node_t;
struct node;
node_t* transform_input(FILE *finput, node_t *root);
//line 21
node_t* bst_insert(node_t *root, record_t* data);
//line 24
node_t* bst_create_node(node_t* root, record_t* data);
node_t* bst_search(node_t* root, char* name_keyword, int* numcomparison);
void search_then_print(char* keyword, node_t* root, FILE* foutput, \
int* numcomparison);
void freeTree(node_t *root);
void print_record(FILE* foutput, node_t* targetnode, char* keyword,\
int* numcomparison);
//line 42
list_t *insert_at_foot(list_t *list, record_t *datarecord);
//line 45
list_t *create_empty_list(void);
//line 48
void free_list(list_t* list);
我已经在线查看了讨论内容,但是试图进行修复,但是找不到头文件中的错误。
谢谢您的帮助。
答案 0 :(得分:1)
下面,我将使用 struct标记与类型标识符。我在Google上搜索了一下,发现Wikipedia上的(IMHO)解释非常容易。
一些程序员老兄提到了这一点,但似乎您不理解他的观点。因此,我对此进行了详细说明:
这是有效的:
struct Node {
struct Node *pNext;
};
,可以与struct
一起使用:
void insert_after(struct Node *pNode);
对于那些懒惰地总是struct
来键入typedef
的人,可以提供帮助:
struct Node {
struct Node *pNext;
};
typedef struct Node Node;
这看起来可能令人困惑,但是编译器将struct
标签和类型分隔在单独的列表中。因此,第一Node
和第二typedef struct Node {
struct Node *pNext;
} Node;
都不是标识符“冲突”。
两者都可以一次完成:
struct
假设另一种情况不使用类型的“递归”,甚至可以忽略typedef struct {
int year, month, day;
} Date;
标签:
struct
这是struct
类型,可以不使用typedef struct record_t;
关键字而专门使用。
我认为这是在写作时打算的
struct
,但是编译器不会按照编写者的预期来解释它。编译器将其读取为record_t
,带有标签warning: useless storage class specifier in empty declaration
和缺少的类型标识符。
这就是我的阅读方式
typedef
(这有助于知道static
在编译器中像extern
和error: expected declaration specifiers or '...' before 'record_t'
一样在语法上进行处理,因此,尽管对于每个人来说,这似乎并不是很明显,但它被视为存储类。)
我必须承认我不知道如何解释
typedef
但是我会忽略这一点,只解决struct
中的弱点(并将错误记为后续错误。)
我还必须承认,我不知道如何使用匿名struct
来解决这个问题,并且想出#include <stdio.h>
/* typedef for an incomplete struct */
typedef struct Date Date;
/* use incomplete struct type for prototype of function */
void printDate(Date *pDate);
/* define the complete struct */
typedef struct Date {
int year, month, day;
} Date;
/* implementation of function */
void printDate(Date *pDate)
{
printf("%04d/%02d/%02d", pDate->year, pDate->month, pDate->day);
}
/* check this out */
int main(void)
{
Date date = { 2018, 9, 3 };
printDate(&date);
return 0;
}
带有标签的想法,该标签可以“重用”作为类型标识符:
2018/09/03
输出:
winfroms
答案 1 :(得分:0)
声明:
typedef struct record_t;
typedef struct list_t;
缺少类型名称。
应该是:
typedef struct record record_t;
typedef struct list list_t;