2013-10-27 225 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等。

相關問題