8

私は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;

私は何を間違っていますか?

日付.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);
  }
}

日付.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_ */
4

3 に答える 3

12

あなたはstruct datedate.cで定義していますが、datetest.cはそれが何であるかわかりません。代わりに date.h で宣言してください。現在、これは不透明な型です。date.h を含むものはすべて、それへのポインターを作成できますが、メンバーにアクセスすることはできません。

于 2013-10-27T18:24:51.483 に答える
3
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.hstruct datedatenote: 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ます。

于 2013-10-27T18:25:27.670 に答える
0

プログラムstruct dateはファイル内で を宣言します。.cつまり、ファイル内で宣言されたものにのみアクセスでき.hます。

を宣言できる理由は、それを にし、コンパイラがすべてのポインターのサイズを認識しているdateためです。pointerただし、 a のメンバーについては何も知らないため、 などのdateメンバーを呼び出すことができませんd->year, d->month, d->day。これらのメンバーを呼び出すと、エラーが発生します。

year1 つの代替方法は、month、 などを返すいくつかのインターフェイス関数を作成することです。

あなたはおそらくデータ構造クラスにいて、ヘッダー ファイルが提供されており、変更しないように指示されているためです。ヘッダーファイルのみで関数を呼び出し、"passed"期待どおりに機能し、プログラムがセグメンテーション違反を起こさなかったことを示すようなものを出力し、などを呼び出す必要なくそのままにしておきyearますmonth

于 2013-10-27T18:26:07.680 に答える