zoukankan      html  css  js  c++  java
  • Cstruct errorField 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.指针不需要一个完整的类定义来声明

  • 相关阅读:
    unix操作系统一些笔记
    安装SQL SERVER2008 R2出现的几个问题
    js中的类型和函数参数传递类型问题
    js一些要点
    百度2015年前端笔试题(南京区域)
    js 函数中的 this 问题
    html细节
    前后台读取Web.config中的值的方法
    使用的 SQL Server 版本不支持数据类型“datetime2”.
    物料主数据MRP4中的独立/集中
  • 原文地址:https://www.cnblogs.com/yzz0110/p/7853281.html
Copyright © 2011-2022 走看看