C: 类型定义不完整 'struct date' 错误

标签 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;

我做错了什么?

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

数据测试.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_ */

最佳答案

您在 date.c 中定义 struct date,datetest.c 不知道它是什么。改为在 date.h 中声明它。目前它是一种不透明类型 - 任何包含 date.h 的内容都可以指向它,但不能访问其成员。

关于C: 类型定义不完整 'struct date' 错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/19621677/

相关文章:

c - 无法按顺序初始化数组,以避免代码冗长

c - 这个类型转换是什么意思? (int (*)[10])

c - 值没有保留在函数之外

c - 如何在取消重复变量的同时将一个数组更改为另一个数组?

c - 模运算结果的符号?

c++ - 内联函数的概念是什么以及它与宏有何不同?

c - 二进制字符指针中的字节数

c - Scanf 段错误

ios - 使用 clang xcode 9 为 iOS armv7+arm64 编译 C 代码

c - X11: 将像素值转换为 RGB