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

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. delphi 线程 TTask

    http://docwiki.embarcadero.com/Libraries/Seattle/en/System.Threading.TTask http://docwiki.embarcader ...

  2. TRegEx 正则表达式

    TRegEx #include <System.RegularExpressions.hpp> void __fastcall TForm1::Button1Click(TObject * ...

  3. Squid Linux 代理服务器

    简介: Squid 是 Linux/Unix 平台下最为流行的高性能免费应用层代理服务器,它具有权限管理灵活.性能高和效率快的特点. 代理服务器可以提供文件缓存.复制和地址过滤等服务,充分利用有限的出 ...

  4. jquery UI 的 datapicker 中文汉化问题

    这个问题自己上网上百度的了很多的方法都没有 具体一点的东西,好在自己没有放弃 从昨天到现在,自己 摸索了很久终于找到了汉化的方法了,希望可以帮助到像我这样刚入web门的小白. 废话不说了直接来干货吧! ...

  5. Linux实战教学笔记36:PHP服务缓存加速深度优化实践

    一,PHP缓存加速器介绍与环境准备 1.1 PHP缓存加速器介绍 1.1.1 操作码介绍及缓存原理 当客户端请求一个PHP程序时,服务器的PHP引擎会解析该PHP程序,并将其编译为特定的操作码(Ope ...

  6. python's fnmatch&glob&os.listdir

    [python's fnmatch&glob&os.listdir] fnmatch: fnmatch只有4种special character,用于提供和shell中一样的文件名的匹 ...

  7. MyCat的安装及基本使用(MySQL分库分表)

    1.Mycat的简介 1.1 数据库集群产生的背景 如今随着互联网的发展,数据的量级也是成指数的增长,从GB到TB到PB.对数据的各种操作也是愈加的困难,传统的关系性数据库已经无法满足快速查询与插入数 ...

  8. make: *** No rule to make target `build', needed by `default'. Stop.

    [root@xx nginx-1.8.0]# makemake: *** No rule to make target `build', needed by `default'.  Stop. [ro ...

  9. hdcloud SOA架构

    SOA是一个范式   多租户技术 一个租户可以是任何一个应用--或者是企业内部应用,或外部应用,它需要有自己的安全的和排他的虚拟计算环境.该环境包含了从存储到用户界面的所有或者某些选定的企业架构层.所 ...

  10. Linux的kickstart安装详解

    Linux的kickstart安装详解 一.什么是kickstart? kickstart安装是redhat开创的按照你设计好的方式全自动安装系统的方式.安装方式可以分为光盘.硬盘.和网络.此文将以网 ...