【发布时间】:2015-08-09 11:12:36
【问题描述】:
这在 C++ 中会出错,但在 C 中不会:
typedef struct nodes
{
int data;
struct node *next;
}node;
它在 C++ 中给出以下错误。
/home/DS cpp/linkedlist.cpp|10|error: conflicting declaration ‘typedef struct nodes node’|
/home/DS cpp/linkedlist.cpp|9|error: ‘struct node’ has a previous declaration as ‘struct node’|
||=== Build failed: 2 error(s), 0 warning(s) (0 minute(s), 0 second(s)) ===|
为了让它在 C++ 中工作,我必须将其更改为:
typedef struct node
{
int data;
struct node *next;
}node;
我不明白为什么会这样,我想知道 C 和 C++ 中的执行顺序以便我理解。
【问题讨论】:
-
你知道你不需要在 C++ 中使用
typedef,对吧? “让它工作”的正确方法是struct node { int data; node* next;};。 -
“执行顺序”与什么有什么关系?
-
@alk,在 C 中有效,
struct node*声明了一个新的类型,不同于struct nodes -
@JonathanWakely, huhu, 代码块... :}
-
@MikeHousky:允许声明指向不完整类型的指针是必要的。