error: field 'b' has imcomplete type
在下面的程序中,在编译时会遇到下面的错误:


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的更多相关文章
- 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 ...
- 网站部署后Parser Error Message: Could not load type 的解决方案
asp.net 的Webproject 项目是在64bit机上开发,默认选项发布后,部署到32bit的服务器上,出现Parser Error Message: Could not load type的 ...
- 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 ...
- error “base class has incomplete type”
error "base class has incomplete type" 如果base.h是你的基类,那么在子类derive中,写成如下形式: class base; clas ...
- error: expected constructor, destructor, or type conversion before '.' token
今天写代码是遇到这样一个问题error: expected constructor, destructor, or type conversion before '.' token:立马网上查,原来是 ...
- protocol error, got 'n' as reply type byte
centos6.5上安装redis3.2版本,本地访问redis报错protocol error, got 'n' as reply type byte 解决办法 在redis配置文件redis.co ...
- 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 [ ...
- ERROR: Field * doesn't have a default value
ERROR: Field 'status' doesn't have a default value 今天做项目,在插入数据时出现了这个从没遇到的异常,看了98%的异常分析都是针对组件id出现了类似的 ...
- protocol error, got 'n' as reply type byte + redis如何后台启动
其它机子的PHP访问redis爆“protocol error, got 'n' as reply type byte ”错误 解决办法: 在redis配置文件redis.conf中注释掉bind配置 ...
随机推荐
- JS的常用开发框架有哪些?
JS的开发框架有哪些? Yui-ext 基于Yahoo UI的扩展包yui-ext是具有cs风格的web用户界面组件,能实现复杂的Layou布局,界面效果可以和backbase比美,而且使用纯Java ...
- 「小程序JAVA实战」小程序和后台api通信(28)
转自:https://idig8.com/2018/08/19/xiaochengxujavashizhanxiaochengxuhehoutaiapitongxin28/ 开发最重要的就是实操! 小 ...
- delphi 浮点 精度
double 没有问题, Single有问题 '0.7' 0.69999999999999996 Single; // 4 byte real Double; // 8 byte real
- 跟我学算法-tensorflow 实现线性拟合
TensorFlow™ 是一个开放源代码软件库,用于进行高性能数值计算.借助其灵活的架构,用户可以轻松地将计算工作部署到多种平台(CPU.GPU.TPU)和设备(桌面设备.服务器集群.移动设备.边缘设 ...
- 移动端web页面input限制只能输入数字
<input type="number" pattern="[0-9]*" /> 如上所示,在安卓端设置input类型为number,可限制键盘只输 ...
- 数组和集合(二):List集合的使用总结
一.概述 · 继承collection接口,List代表一个元素有序.且可重复(包括null)的集合,集合中的每个元素都有其对应的顺序索引 · List默认按元素的添加顺序设置元素的索引 · 提供了一 ...
- 使用OpenSsl自己CA根证书,二级根证书和颁发证书(亲测步骤)
---恢复内容开始--- 一.介绍 企业自用, 到证书机构签发证书的费用和时间等都可以省下..... SSl证书的背景功用.......(省略万字,不废话) 可以参考: SSL证书_百度百科 X509 ...
- new 约束
[new 约束] new 约束指定泛型类声明中的任何类型参数都必须有公共的无参数构造函数.如果要使用 new 约束,则该类型不能为抽象类型. 当泛型类创建类型的新实例,请将 new 约束应用于类型参数 ...
- O(n)求中位数和第k大数
解题关键:模板与思路.面试题 #include<cstdio> #include<cstring> #include<algorithm> #include< ...
- linux 安装天气插件
1. 用命令 sudo apt install gnome-shell-extension-weather 安装插件 但是在那之前你安装了 GNOME Shell Extensions 如果没有安装 ...