我开始学习C并遇到了一个问题。
我创建了一个日期ADT并希望测试它:)
基本上,我想从标准输入读取一个字符串,将其转换为日期并在标准输出上打印出来。
编译这些文件后,出现以下错误:
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;
我做错了什么?
date.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:
#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:
#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_ */
答案 0 :(得分:10)
你在date.c中定义struct date
,datetest.c不知道它是什么。在date.h中声明它。目前它是一个opaque类型 - 任何包含date.h的东西都可以指向它,但不能访问成员。
答案 1 :(得分:2)
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;
当编译器解析date.h
时,它会检测到date.h
没有struct date
但它使用date
。所以它会引发注释note: forward declaration of 'struct date'
在datetest.c
中,您已包含date.h
,但未包含date.c
中的实际定义,且编译器无法检测到该类型。这就是为什么它会抛出错误
error: incomplete definition of type 'struct date'
要解决此问题,
struct date {
int day;
int month;
int year;
};
将其移至date.h
文件。
答案 2 :(得分:0)
您的程序在struct date
文件中声明.c
,这意味着您只能访问.h
文件中声明的内容。
您可以声明date
的原因是因为您将其设为pointer
并且编译器知道所有指针的大小。但是,它对date
的成员一无所知,因此,您无法呼叫d->year, d->month, d->day
等成员。致电这些成员会给您带来错误。
另一种方法是制作一些返回year
,month
等的接口函数。
由于您可能位于数据结构类中,因此已为您提供了头文件,并且已指示不要更改它。我只会在头文件中调用函数,并在它们按预期工作时打印"passed"
之类的内容并显示程序没有分段错误,并将其留在那里而无需调用year
,{ {1}}等等。