C-struct error-Field has incomplete type

  真是受不了cnblogs了,贴个代码麻烦的要死,写完这篇准备抽时间自己搭博客了。

  我经常在写一个结构体的时会想,比如写一个链表:

struct Node
{
  int value;
  Node* prev, *next;
};

仔细想想如果把Node* prev, *next; 换成Node prev,next 会怎样,也就是指针类型换成变量类型,然后->换成 . 操作就可以了,这样想是没问题,但有两个问题:

一是,假设第一个链表结点是头结点(不包含实际数据,方便后续操作),那么这个结点的一个成员变量prev就不好赋空,因为NULL是赋给指针的,int a=NULL编译器会警告.

二是根本原因,The error means that you try and add a member to the struct of a type that isn't fully defined yet, so the compiler cannot know its size in order to determine the objects layout.

In you particular case, you try and have struct Cat hold a complete object of itself as a member (the mother field). That sort of infinite recursion in type definition is of course impossible.

Nevertheless, structures can contain pointers to other instances of themselves.

这是stackoverflow上的一段解释,就是说这样用会造成无限递归

The recursion is in the type containing itself completely as a member.而这个递归就是在一个type中完全以它为类型作为一个成员。

struct Node

{

  Node temp;
};

比如这样就会造成temp里面又有一个temp 如此循环却不能退出. It doesn't go into recursive calls. It stops and emits an error because it recognizes the type contains itself as membe

但不会进到无限递归调用中,编译器停止并报错如果它认出这个类包含这个类本身作为成员变量;

Why struct Cat *children member does not make the type recursive?  指针类型为什么不会造成循环

because it's a pointer. You don't need the complete type definition to declare a pointer to it.指针不需要一个完整的类定义来声明

 

转载于:https://www.cnblogs.com/yzz0110/p/7853281.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值