错误:无效使用未定义类型'struct Item'

时间:2017-07-10 02:25:04

标签: c struct dynamic-allocation

以下是相关结构。请注意,它们位于同一文件的标题中。

typedef enum {AAA = 0, BBB = 1, CCC = 2, DDD = 3} Subject;

typedef struct item {
    Subject sub;
    int n, h;
    char title[1024];
} Item;

struct Item* Collection = NULL;

在main中,我为Collection分配空间:

Collection = malloc(sizeof(Item*));

然后,在我的main中,我有一个调用insert()函数的switch语句,该函数如下:

void course_insert() {
    Collection = realloc(Collection,(sizeof(Collection) + sizeof(Iem)));
    printf("What is the subject? (AAA=0, BBB=1, CCC=2, DDD=3)? ");
    scanf("%u", Collection[count].sub);
    printf("What is the number (e.g 20)? ");
    scanf("%d", Collection[count]->n);
    printf("How many hours (e.g. 3)? ");
    scanf("%d", Collection[count]->h);
    printf("What is the name of the item? ");
    scanf("%s", Collection[count]->title);   
    count++;
}

我收到一个错误,说这是对未定义类型'struct Item'的无效使用,但我可以在其他地方使用它。谁能发现问题是什么?

1 个答案:

答案 0 :(得分:3)

typedef语句表示typedef struct item Item。这意味着您已定义struct itemItem,但未定义struct Item。请尝试使用struct itemItem

相关问题