簡體   English   中英

C:類型'struct date'錯誤的定義不完整

[英]C: incomplete definition of type 'struct date' error

我開始學習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_ */

你在date.c中定義struct date ,datetest.c不知道它是什么。 在date.h中聲明它。 目前它是一個opaque類型 - 任何包含date.h的東西都可以指向它,但不能訪問成員。

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

您的程序在.c文件中聲明struct date ,這意味着您只能訪問.h文件中聲明的內容。

您可以聲明date的原因是因為您將其pointer並且編譯器知道所有指針的大小。 但是,它對date的成員一無所知,因此,您無法像d->year, d->month, d->day等那樣調用成員。調用這些成員會給您錯誤。

另一種方法是制作一些返回yearmonth等的接口函數。

由於您可能位於數據結構類中,因此已為您提供了頭文件,並且已指示不要更改它。 我只會在頭文件中調用函數,並在它們按預期工作時打印"passed" ,並顯示程序沒有分段錯誤,並將其留在那里而不需要調用yearmonth等。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM