C语言 C: 'struct date' 类型的定义不完整错误
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/19621677/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
C: incomplete definition of type 'struct date' error
提问by chuckfinley
i started learning C and just ran into an issue.
我开始学习 C 并遇到了一个问题。
I created a date ADT and would like to test it out :)
我创建了一个日期 ADT 并想对其进行测试:)
basically, i would like to read in a string from standard input, convert it to the date and print it out on standard output.
基本上,我想从标准输入中读取一个字符串,将其转换为日期并在标准输出上打印出来。
after compiling these files i got the following errors:
编译这些文件后,我收到以下错误:
datetest.c:15:45: error: incomplete definition of type 'struct date'
printf("Year: %d Month: %d Day: %d", d->year, d->month, d->day);
~^
./date.h:4:16: note: forward declaration of 'struct date'
typedef struct date Date;
What am i doing wrong?
我究竟做错了什么?
date.c:
日期.c:
#include "date.h"
#include <stdlib.h>
#include <stdio.h>
#include <string.h>
struct date {
int day;
int month;
int year;
};
/*
* date_create creates a Date structure from `datestr`
* `datestr' is expected to be of the form "dd/mm/yyyy"
* returns pointer to Date structure if successful,
* NULL if not (syntax error)
*/
Date *date_create(char *datestr) {
Date *d = (Date *)malloc(sizeof(Date));
const char delimiter[2] = "/";
char *token;
if (d != NULL) {
token = strtok(datestr, delimiter);
d->day = atoi(token);
token = strtok(NULL, delimiter);
d->month = atoi(token);
token = strtok(NULL, delimiter);
d->year = atoi(token);
//printf("Day: %d Month: %d Year: %d\n", d->day, d->month, d->year);
//printf("Day: %p Month: %p Year: %p\n", *d->day, *d->month, *d->year);
}
return d;
};
/*
* date_duplicate creates a duplicate of `d'
* returns pointer to new Date structure if successful,
* NULL if not (memory allocation failure)
*/
Date *date_duplicate(Date *d) {
Date *dd = (Date *)malloc(sizeof(Date));
if (dd != NULL) {
dd->day = d->day;
dd->month = d->month;
dd->year = d->year;
}
return dd;
};
/*
* date_compare compares two dates, returning <0, 0, >0 if
* date1<date2, date1==date2, date1>date2, respectively
*/
int date_compare(Date *date1, Date *date2) {
if (date1->year < date2->year)
return -1;
else if (date1->year > date2->year)
return 1;
else {
if (date1->month < date2->month)
return -1;
else if (date1->month > date2->month)
return 1;
else {
if (date1->day < date2->day)
return -1;
else if (date1->day > date2->day)
return 1;
else
return 0;
}
}
};
/*
* date_destroy returns any storage associated with `d' to the system
*/
void date_destroy(Date *d) {
if (d != NULL)
free(d);
};
datetest.c:
日期测试.c:
#include "date.h"
#include <stdlib.h>
#include <stdio.h>
#include <string.h>
int main() {
Date *d;
char buf[1024], *s;
while (fgets(buf, sizeof(buf), stdin) != NULL) {
if (!(d = date_create(buf))) {
fprintf(stderr, "Unable to create a date.\n");
return -1;
}
printf("Year: %d Month: %d Day: %d", d->year, d->month, d->day);
}
}
date.h:
日期.h:
#ifndef _DATE_H_INCLUDED_
#define _DATE_H_INCLUDED_
typedef struct date Date;
/*
* date_create creates a Date structure from `datestr`
* `datestr' is expected to be of the form "dd/mm/yyyy"
* returns pointer to Date structure if successful,
* NULL if not (syntax error)
*/
Date *date_create(char *datestr);
/*
* date_duplicate creates a duplicate of `d'
* returns pointer to new Date structure if successful,
* NULL if not (memory allocation failure)
*/
Date *date_duplicate(Date *d);
/*
* date_compare compares two dates, returning <0, 0, >0 if
* date1<date2, date1==date2, date1>date2, respectively
*/
int date_compare(Date *date1, Date *date2);
/*
* date_destroy returns any storage associated with `d' to the system
*/
void date_destroy(Date *d);
#endif /* _DATE_H_INCLUDED_ */
回答by Kevin
You're defining struct datein date.c, datetest.c has no idea what it is. Declare it in date.h instead. Currently it's an opaque type - anything that includes date.h can make a pointer to it, but can't access the members.
你struct date在 date.c 中定义,datetest.c 不知道它是什么。改为在 date.h 中声明它。目前它是一个不透明的类型——任何包含 date.h 的东西都可以指向它,但不能访问成员。
回答by thefourtheye
datetest.c:15:45: error: incomplete definition of type 'struct date'
printf("Year: %d Month: %d Day: %d", d->year, d->month, d->day);
~^
./date.h:4:16: note: forward declaration of 'struct date'
typedef struct date Date;
When the compiler parses date.h, it detects that date.hdoesnt have struct datebut it uses date. So it throws that note note: forward declaration of 'struct date'
当编译器解析时date.h,它检测到date.h没有struct date但它使用date. 所以它抛出那个音符note: forward declaration of 'struct date'
In datetest.cyou have included date.hbut not the actual definition which is in date.cand the compiler is not able to detect the type. Thats why it throws the error
在datetest.c已包含date.h但不是实际的定义,这是在date.c和编译器是无法检测的类型。这就是它抛出错误的原因
error: incomplete definition of type 'struct date'
To fix this,
为了解决这个问题,
struct date {
int day;
int month;
int year;
};
Move this to date.hfile.
将其移至date.h文件。
回答by Leonardo
Your program declares the struct datein the .cfile which means you only have access to the things declared in the .hfile.
您的程序struct date在.c文件中声明了,这意味着您只能访问.h文件中声明的内容。
The reason you can declare the dateis because you make it a pointerand the compiler knows the size of all pointers. However, it knows nothing about the members of a dateand, consequently, you are unable to call members like d->year, d->month, d->day, etc. Calling these members gives you your error.
您可以声明 a 的原因date是因为您将其设为 apointer并且编译器知道所有指针的大小。但是,它对 a 的成员一无所知date,因此,您无法调用诸如 之类的成员d->year, d->month, d->day。调用这些成员会给您带来错误。
One alternative is to make some interface functions which return the year, month, etc.
一种选择是做一些接口函数,其返回year,month等等。
Since you are probably in a data structures class, and the header file is provided for you and have been instructed not to alter it. I would just call functions in the header file ONLY, and print things like "passed"when they work as expected and show the program did not segmentation fault, and leave it at that without needing to call year, month, etc.
由于您可能在数据结构类中,并且为您提供了头文件并且已指示不要更改它。我只想调用函数只在头文件和打印之类的东西"passed"时,他们的工作预期,并显示该程序没有分割的错,离开它,而无需调用year,month等等。

