2013-02-26 286 views
0

当我尝试编译此代码时,我从编译器中收到了一堆奇怪的错误。C要求一个结构或联合至少有一个成员

我只是想写一个基本的链接列表,并在自身运行的头文件编译罚款,但是当我编译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' 

我很困惑,因为它看起来像是找到了两个文件,但是它仍然遇到了头文件的问题,尽管它可以单独运行。 任何帮助将是伟大的。 感谢

回答

5

的错误信息是陌生的,但

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

LinkedList没有在定义中的已知类型,它只是认识的定义是完整的,它必须是

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

头文件不是自己编译的。他们唯一的用处是在别处有#included。

在头的错误是这一部分:

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

试试这个,这是比替代建议清洁剂(编辑:我看到墨尔波墨涅我之前提到这一点):

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

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

你会查找是否/何时移至C++,您可以删除typedef行。

如果你已经相互参照结构,将所有类型定义到顶部,例如,除了LinkedList前加入struct

typedef Type1 Type1; 
typedef Type2 Type2; 

struct Type1 
{ 
    Type2* link; 
}; 

struct Type2 
{ 
    Type1* link; 
}; 
0

,您可能需要显式地设置了“编译为”选项,以C++中视觉工作室。

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

相关问题