1

当我尝试编译这段代码时,我从编译器中得到了一堆奇怪的错误。

我只是想写一个基本的链表,头文件在单独运行时编译得很好,但是当我编译 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'

我很困惑,因为它看起来好像找到了两个文件,但是即使它单独工作正常,它的标题也有问题。任何帮助都会很棒。谢谢

4

4 回答 4

7

错误信息很奇怪,但是

typedef struct LinkedList
{
    LinkedList *next;
    Data *data;
} LinkedList;

LinkedList不是定义中的已知类型,只有定义完成后才知道,必须是

typedef struct LinkedList
{
    struct LinkedList *next;
    Data *data;
} LinkedList;
于 2013-02-26T22:52:17.377 回答
4

头文件不是自己编译的。它们的唯一用途是在#include其他地方使用。

标题中的错误是这部分:

typedef struct LinkedList  // <- defines the type 'struct LinkedList'
{
    LinkedList *next;      // <- attempts to use the type 'LinkedList', which isn't defined at this point
} LinkedList;              // <- defines the type 'LinkedList', or it would if the previous code were correct

有几种可能的方法来解决这个问题:

// forget about 'LinkedList'; just use 'struct LinkedList' everywhere
struct LinkedList {
    struct LinkedList *next;
};

// use 'struct LinkedList' in its own definition but provide typedef to users
struct LinkedList {
    struct LinkedList *next;
};
typedef struct LinkedList LinkedList;

// like the previous version, but combine struct + typedef
typedef struct LinkedList {
    struct LinkedList *next;
} LinkedList;

还有我最喜欢的:

// establish alias first, then use it everywhere
typedef struct LinkedList LinkedList;
struct LinkedList {
    LinkedList *next;
};
于 2013-02-26T22:52:24.820 回答
0

试试这个,它比其他建议更干净(编辑:我看到 melpomene 在我之前提到过这个):

typedef struct Data Data;
struct Data
{
    char *title;
};

typedef struct LinkedList LinkedList;
struct LinkedList
{
    LinkedList *next;
    Data *data;
};

您会发现如果/当您继续使用 C++ 时,您可以删除 typedef 行。

如果您有相互引用的结构,请将所有 typedef 移到顶部,例如,

typedef Type1 Type1;
typedef Type2 Type2;

struct Type1
{
    Type2* link;
};

struct Type2
{
    Type1* link;
};
于 2013-02-26T22:57:30.060 回答
0

除了添加structbefore LinkedList,您可能需要在 Visual Studio 中将“compile as”选项显式设置为 c++。

我已经为这个 C2016 错误苦苦挣扎了一段时间,最终通过设置选项来修复它。

于 2013-07-19T03:00:17.967 回答