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

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. mongodb(二)

    数据准备: var persons = [{ name:"jim", age:25, email:"75431457@qq.com", c:89,m:96,e: ...

  2. leetcode278

    /* The isBadVersion API is defined in the parent class VersionControl. bool IsBadVersion(int version ...

  3. 教你用一行Python代码实现并行(转)

    教你用一行Python代码实现并行 本文教你通过一行Python实现并行化. Python在程序并行化方面多少有些声名狼藉.撇开技术上的问题,例如线程的实现和GIL,我觉得错误的教学指导才是主要问题. ...

  4. UNIX网络编程——socket的keep-alive(转)

    第一部分 [需求] 不影响服务器处理的前提下,检测客户端程序是否被强制终了. [现状] 服务器端和客户端的Socket都设定了keepalive属性. 服务器端设定了探测次数等参数,客户端.服务器只是 ...

  5. 使用copy函数输出容器中的内容

    container<type> c; 输出语句为:copy(c.begin(), c.end(), ostream_iterator<type>(cout, " &q ...

  6. 配置python的豆瓣source

    sunny@sunny-ThinkPad-T450:~$ mkdir ~/.pip sunny@sunny-ThinkPad-T450:~$ gedit ~/.pip/pip.conf [global ...

  7. <mvc:annotation-driven />讲解

    <mvc:annotation-driven />是一种简写形式,完全可以手动配置替代这种简写形式,简写形式可以让初学者快速应用默认配置方案.<mvc:annotation-driv ...

  8. python pipenv 包管理

    原文链接:https://robots.thoughtbot.com/how-to-manage-your-python-projects-with-pipenv 翻译者:Jiong 在thought ...

  9. ZPP002M可重复执行

    AutoOutToSAP一直未回写完成,查看得知最后一步执行时间超长, stop job ZPP002M可重复执行

  10. zabbix主机自动注册

    一.主机自动注册的流程 zabbix agent指定server active主动自己的信息提供给zabbix_server,zabbix_server根据提供的信息自动添加主机,方便. 二. lin ...