当我尝试编译这段代码时,我从编译器中得到了一堆奇怪的错误。
我只是想写一个基本的链表,头文件在单独运行时编译得很好,但是当我编译 c 文件时,一切都崩溃了
链表.h
typedef struct Data
{
char *title;
} Data;
typedef struct LinkedList
{
LinkedList *next;
Data *data;
} LinkedList;
LinkedList *createnew(void);
void destroylist(LinkedList *old);
Data *adddata(void);
void destroydata(Data *old);
链表.c
#include <stdlib.h>
#include "linkedlist.h"
LinkedList *createnew(void) {
LinkedList *newnode = (LinkedList *) malloc(sizeof(LinkedList));
newnode->data = adddata();
newnode->next = NULL;
return newnode;
}
void destroylist(LinkedList *old) {
destroydata(old->data);
free(old);
return;
}
Data *adddata(void) {
Data *newdata = (Data *) malloc(sizeof(Data));
newdata->title = NULL;
return newdata;
}
void destroydata(Data *old) {
free(old->title);
free(old);
return;
}
最后编译器吐出什么
linkedlist.h(8): error C2016: C requires that a struct or union has at least one member
linkedlist.h(8): error C2061: syntax error : identifier 'LinkedList'
linkedlist.h(10): error C2059: syntax error : '}'
linkedlist.h(12): error C2143: syntax error : missing '{' before '*'
linkedlist.h(13): error C2143: syntax error : missing ')' before '*'
linkedlist.h(13): error C2143: syntax error : missing '{' before '*'
linkedlist.h(13): error C2059: syntax error : ')'
linkedlist.c(4): error C2143: syntax error : missing '{' before '*'
linkedlist.c(5): error C2065: 'LinkedList' : undeclared identifier
linkedlist.c(5): error C2065: 'newnode' : undeclared identifier
linkedlist.c(5): error C2059: syntax error : ')'
linkedlist.c(6): error C2065: 'newnode' : undeclared identifier
linkedlist.c(6): error C2223: left of '->data' must point to struct/union
linkedlist.c(7): error C2065: 'newnode' : undeclared identifier
linkedlist.c(7): error C2223: left of '->next' must point to struct/union
linkedlist.c(8): error C2065: 'newnode' : undeclared identifier
linkedlist.c(8): warning C4047: 'return' : 'int *' differs in levels of indirection from 'int'
linkedlist.c(11): error C2143: syntax error : missing ')' before '*'
linkedlist.c(11): error C2143: syntax error : missing '{' before '*'
linkedlist.c(11): error C2059: syntax error : ')'
linkedlist.c(11): error C2054: expected '(' to follow 'old'
我很困惑,因为它看起来好像找到了两个文件,但是即使它单独工作正常,它的标题也有问题。任何帮助都会很棒。谢谢