2013-10-27 232 views
2

我开始学习C并刚刚遇到问题。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_ */ 

回答

7

你定义在date.c struct date,datetest.c不知道它是什么。而是用date.h声明它。目前它是一个不透明的类型 - 任何包含date.h的东西都可以指向它,但不能访问成员。

+0

一个代码示例吗? :) – chuckfinley

+0

移动'结构日期int day; int月; int year; }; '部分放入​​头文件。 – Kevin

+0

在任务定义中,它说我不应该编辑date.h文件,只是date.c.有没有办法解决? – chuckfinley

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文件。

+0

在我给我的任务,我只应该只对date.c和datetest.c工作。有没有办法解决? – chuckfinley

+0

@martynas Nope :(你可能需要在所有需要它的'c'文件中定义'date'和'typedef'。简单的方法是将'date'的定义移动到'date.h' – thefourtheye

+0

我得到现在有一个错误:./datetest.o:格式错误的Mach-o文件 – chuckfinley

0

您的程序在.c文件中声明struct date这意味着您只能访问.h文件中声明的事物。

您可以声明date的原因是因为您将其设置为pointer并且编译器知道所有指针的大小。但是,它对date的成员一无所知,因此,您无法呼叫d->year, d->month, d->day等成员。调用这些成员会给您提供错误。

一种选择是做一些接口函数,其返回yearmonth

因为你可能在一个数据结构类,并为您提供的头文件,并已指示不改变它。我只是在头文件中调用函数,并在按预期工作时打印诸如"passed"之类的东西,并显示程序没有分段错误,并且不需要调用yearmonth等。

相关问题