在下面的程序中,在编译时会遇到下面的错误:

error: field 'b' has incomplete type

域b是一个不完备的类型,即class B的声明不完备

 #include <iostream>
using namespace std; class A
{
private:
class B b;
public:
A(){cout<<"this is A constructor!"<<endl;}
}; class B
{
public:
B(){cout<<"this is B constructor!"<<endl;}
}; int main()
{
A a;
return ;
}

分析:

出现该错误的原因是在声明class A的成员变量b时, b的类型 class B 还没有定义。

类或结构体的前向声明只能用来定义指针对象,当编译到 class B b; 时还没有发现定义,

不知道该类或者结构的内部成员,没有办法具体的构造一个对象。

Debug:

将class B的定义放到class A之前即可编译通过

那么还有哪些情况下会出现这样的情况呢?

1. 在使用设计模式时,我们时常会用到工厂模式,当我们需要添加工序类的时候,往往习惯现在工厂类中定义工序的对象,然后在工厂类之后定义工序类,从而出现本文中的错误。

比较好的解决方法是,将工序类单独存放到一个头文件中,然后在工厂类所在文件中引用该头文件。

2. 在使用STL容器的时候,往往忘记添加相应的头文件。如#include <vector>; #include <list>; #include <deque>等

error: field 'b' has imcomplete type的更多相关文章

  1. A const field of a reference type other than string can only be initialized with null Error [duplicate]

    I'm trying to create a 2D array to store some values that don't change like this. const int[,] hiveI ...

  2. 网站部署后Parser Error Message: Could not load type 的解决方案

    asp.net 的Webproject 项目是在64bit机上开发,默认选项发布后,部署到32bit的服务器上,出现Parser Error Message: Could not load type的 ...

  3. Huge CSV and XML Files in Python, Error: field larger than field limit (131072)

    Huge CSV and XML Files in Python January 22, 2009. Filed under python twitter facebook pinterest lin ...

  4. error “base class has incomplete type”

    error "base class has incomplete type" 如果base.h是你的基类,那么在子类derive中,写成如下形式: class base; clas ...

  5. error: expected constructor, destructor, or type conversion before '.' token

    今天写代码是遇到这样一个问题error: expected constructor, destructor, or type conversion before '.' token:立马网上查,原来是 ...

  6. protocol error, got 'n' as reply type byte

    centos6.5上安装redis3.2版本,本地访问redis报错protocol error, got 'n' as reply type byte 解决办法 在redis配置文件redis.co ...

  7. ERROR: cannot launch node of type [teleop/teleop_key]: can't locate node [teleop_key] in package [teleop]

    节点由python写成,编译通过,运行时报错如下: ERROR: cannot launch node of type [teleop/teleop_key]: can't locate node [ ...

  8. ERROR: Field * doesn't have a default value

    ERROR: Field 'status' doesn't have a default value 今天做项目,在插入数据时出现了这个从没遇到的异常,看了98%的异常分析都是针对组件id出现了类似的 ...

  9. protocol error, got 'n' as reply type byte + redis如何后台启动

    其它机子的PHP访问redis爆“protocol error, got 'n' as reply type byte ”错误 解决办法: 在redis配置文件redis.conf中注释掉bind配置 ...

随机推荐

  1. Vim配置:在win10下用vim编译运行C/C++(异步插件管理,一键运行)

    为什么用Vim 重新调配vim,追求尽量简单些. 安装 官网下载 PC: MS-DOS and MS-Windows下的 For modern MS-Windows systems (starting ...

  2. Spring之导入和混合配置

    在典型的Spring应用中,我们可能会同时使用自动化和显式配置.即便你更喜欢通过JavaConfig实现显式配置,但有的时候XML却是最佳的方案.幸好在Spring中,这些配置方案都不是互斥的.你尽可 ...

  3. Struts2 的核心、执行原理

    转自: http://www.cnblogs.com/xiadongqing/p/5240615.html 在学习struts2之前,首先我们要明白使用struts2的目的是什么?它能给我们带来什么样 ...

  4. mybatis 用法分享

    主题 这篇文章主要是记录这2个月我对mybatis的学习以后的一些感触和一些如果我是架构师会怎么在项目里使用mybatis的一些大胆的想法. 感想 1.首先根据之前的学习我已经知道了mybatis g ...

  5. 如何在ArcMap中监听键盘鼠标事件

    昨天有个朋友想要实现一个功能,就是在ArcMap中编辑数据的时候,能够通过快捷键自动设置预定义的属性,比如,选中若干要素,按A键,就自动填充属性,按B键,则又自动填充另外的属性字段. 单就这个功能而言 ...

  6. Scala开发Hadoop示例

    import org.apache.hadoop.conf.{Configuration, Configured}; import org.apache.hadoop.util.{ToolRunner ...

  7. 蓝桥杯练习系统算法训练习题加答案java版本

    附上百度文库的链接:http://wenku.baidu.com/view/afb78d36b42acfc789eb172ded630b1c59ee9bf7

  8. Tp3.1 文件上传到七牛云

    TP3.1 中不支持Composer 就无法用composer 安装 下载历史的SDK https://github.com/qiniu/php-sdk/releases/tag/v7.0.8 下载下 ...

  9. TCP与UDP与HTTP协议

    http:是用于www浏览的一个协议.tcp:是机器之间建立连接用的到的一个协议. 1.TCP/IP是个协议组,可分为三个层次:网络层.传输层和应用层.在网络层有IP协议.ICMP协议.ARP协议.R ...

  10. SpringCloud04 服务配置中心、消息总线、远程配置动态刷新

    1 环境说明 JDK:1.8 MAVENT:3.5 SpringBoot:2.0.5.RELEASE SpringCloud:Finchley.SR1 2 创建服务注册中心(Eureka服务端) 说明 ...