下一个结构项,不完整类型

发布于 2024-09-30 10:21:10 字数 126 浏览 1 评论 0原文

struct node{  
    struct node next;  
    int id;  
}

给出“下一个字段有不完整的类型错误”。

这个结构有什么问题?

struct node{  
    struct node next;  
    int id;  
}

gives "field next has incomplete type error ".

what is wrong with this struct ?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(6

浅紫色的梦幻 2024-10-07 10:21:10

创建自引用数据类型时,您需要使用指针来解决循环问题:

struct node;

struct node {  
    struct node * next;  
    int id;  
}

...应该可以,但在使用它时要注意正确分配内存。

为什么要有指针?考虑一下:struct 定义的要点是,当您说出 node.id 时,编译器可以计算出要分配多少内存以及要访问哪些部分。如果您的 node 结构体包含另一个 node 结构体,编译器应该为给定的 node 分配多少内存?

通过使用指针可以解决这个问题,因为编译器知道为指针分配多少空间。

When creating a self-referential data type, you need to use pointers to get around problems of circularity:

struct node;

struct node {  
    struct node * next;  
    int id;  
}

...should work, but take care to allocate memory correctly when using it.

Why a pointer? Consider this: the point of a struct definition is so that the compiler can figure out how much memory to allocate and what parts to access when you say node.id. If your node struct contains another node struct, how much memory should the compiler allocate for a given node?

By using a pointer you get around this, because the compiler knows how much space to allocate for a pointer.

情栀口红 2024-10-07 10:21:10

如果一个结构体可以包含它自己类型的另一个实例,那么它的大小将是无限的。

这就是为什么它只能包含指向其自身类型的指针

此外,在代码中,结构的大小是未知的,因此编译器无法知道要为其保留多少空间。

If a struct could contain another instance of its own type, its size would be infinite.

This is why it can only contain a pointer to its it own type.

Furthermore, at that point in code, the size of the struct is unknown, so the compiler couldn't know how much space to reserve for it.

゛清羽墨安 2024-10-07 10:21:10
  1. 您需要对节点进行前向声明,因为编译器在处理其定义时还不知道节点。
  2. 您可能想存储一个指针,而不是节点对象本身。

试试这个:

struct node;

struct node{  
    struct node *next;  
    int id;  
};
  1. You need to do a forward-declaration of node, since the compiler doesn't know node yet while it's processing its definition.
  2. You probably meant to store a pointer, not a node object itself.

Try this:

struct node;

struct node{  
    struct node *next;  
    int id;  
};
萌辣 2024-10-07 10:21:10

不完整类型的某些使用是不正确的,例如当您尝试声明不完整类型的对象时。但是,您可以声明一个指向不完整类型的指针(例如)。在这种情况下,这正是这里所需要的:

struct node{  
    struct node *next;  
    int id;  
};

Some uses of incomplete types are ill-formed, such as when you try to declare an object of an incomplete type. However, you can declare a pointer to an incomplete type (for example). In this case that is just what is needed here:

struct node{  
    struct node *next;  
    int id;  
};
心头的小情儿 2024-10-07 10:21:10

问题是,当编译器到达这一行时:

struct node{  
    struct node next;  /* << this line */

编译器实际上不知道什么是struct node,因为您正在定义struct node

一般来说,不能使用未定义或不完整的类型。

The problem is, when the compiler reaches this line:

struct node{  
    struct node next;  /* << this line */

the compiler actually doesn't know what is struct node, because you're defining struct node.

In general, you cannot use a type which is not defined or incomplete.

虐人心 2024-10-07 10:21:10

为了工作,你应该写:

typedef struct _node{
  struct _node* next;
  int           id;
}node; 

In order to work, you should write:

typedef struct _node{
  struct _node* next;
  int           id;
}node; 
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文