【发布时间】:2015-04-15 13:05:53
【问题描述】:
好的,我已经搜索了两天的解决方案,但我找不到我的代码出了什么问题。 ;(
任务很简单:使用 typedef 定义一个新类型,并让一个函数将这个新类型的行从一个文件中读出到这个新类型的数组中。所以我在头文件中的 typedef 现在看起来像这样(我尝试了几种写这个的变体)
// filename: entries.h
#ifndef ENTRIES_H_
#define ENTRIES_H_
#include<time.h>
typedef struct{
char Loginname[25];
time_t RegDate;
unsigned long Highscore;
time_t Hdate;
}typePlayerEntry;
int readPlayerList(char *name, typePlayerEntry *feld);
#endif /* ENTRIES_H_ */
main.c:
//filename: main.c
#include <stdio.h>
#include "entries.h"
int main(void) {
char name[13]="numbers.txt";
typePlayerEntry *pep;
readPlayerList(name, pep);
return 0;
}
我的函数文件看起来像这样(以及显示错误的地方)
//filename: readPlayerList.c
int readPlayerList(char *name, typePlayerEntry *feld) {
return 0;
}
完全忽略不相关的代码。发布的代码可以重现该问题。
程序无法编译,因为函数文件中第二个参数的类型无法识别, - 这很奇怪,因为它在头文件中定义并且也可以在主函数中使用。 这个错误与我的 main.c 中(在这种情况下)类型为 playerEntry 的指针的声明有关。因此,如果我不声明它,则没有错误,尽管我必须声明它才能实际将其提供给函数。为什么到目前为止的解决方案是将entrys.h包含到readPlayerList.c中,而这对于以前的功能来说是不必要的?
我将 eclipse kepler 与 MinGW 一起使用,以防这是编译器问题。
更正了 time.h 缺少的包含并稍微调整了代码。
【问题讨论】:
-
对我来说很合适。 struct和typedef声明都在函数声明前面吗?
-
好的,您已经声明这些出现在头文件中...但是您是否将其包含在使用它的源中?
-
@nneonneo 是的,编辑了我的问题,显示了产生错误的整个文件。
标签: c function struct typedef argument-passing