C要求结构或联合至少有一个成员

时间:2013-02-26 22:47:57

标签: c compiler-errors

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

我只是想编写一个基本的链表,并且头文件在自己运行时编译得很好,但是当我编译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);

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;
}

最后是编译器吐出的内容

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 个答案:

答案 0 :(得分:6)

错误消息很奇怪,但

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

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

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

答案 1 :(得分:3)

头文件不是自己编译的。他们唯一的用途是在其他地方#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;
};

答案 2 :(得分: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;
};

答案 3 :(得分:0)

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

我一直在努力解决这个C2016错误,最后通过设置选项修复了它。