当我尝试编译这段代码时,我从编译器中得到了一堆奇怪的错误.
我只是想编写一个基本的链表,并且头文件在自己运行时编译得很好,但是当我编译c文件时,一切都崩溃了
linkedlist.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);
Run Code Online (Sandbox Code Playgroud)
LinkedList.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;
}
Run Code Online (Sandbox Code Playgroud)
最后是编译器吐出的内容
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'
Run Code Online (Sandbox Code Playgroud)
我很困惑,因为它看起来像找到了两个文件,但它的标题有问题,即使它单独工作.任何帮助都会很棒.谢谢
错误消息很奇怪,但是
typedef struct LinkedList
{
LinkedList *next;
Data *data;
} LinkedList;
Run Code Online (Sandbox Code Playgroud)
LinkedList 在定义中不是已知类型,它只在定义完成后才知道,它必须是
typedef struct LinkedList
{
struct LinkedList *next;
Data *data;
} LinkedList;
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
9297 次 |
| 最近记录: |