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配置 ...
随机推荐
- MySQL多实例介绍
我们前面已经做了MySQL数据库的介绍以及为什么选择MySQL数据库,最后介绍了MySQL数据库在Linux系统下的多种安装方式,以及讲解了MySQL的二进制方式单实例安装.基础优化等,下面给大家讲解 ...
- Spring源码研究--下载-编译-导入eclipse-验证
一,环境配置 操作系统:Unbutu14.04LTS JDK: 1.8.0_40 git: 1.9.1 gradle: 2.2.1 二,源码下载-编译-导入eclipse-验证 1,下载 使用git直 ...
- C#利用反射,遍历获得一个类的所有属性名,以及该类的实例的所有属性的值
转自goldeneyezhang原文 C#利用反射,遍历获得一个类的所有属性名,以及该类的实例的所有属性的值 C#利用反射,遍历获得一个类的所有属性名,以及该类的实例的所有属性的值总结: 对应某个类的 ...
- 【309】◀▶ Windows 相关功能实现
目录: 共享文件夹失败的解决方法 导 栅 添 1. 共享文件夹失败的解决方法 参考:解决“你没有权限访问,请与网络管理员联系” 参考:WIN7局域网文件共享设置方法 2. 导 在 3. 栅 栅 4. ...
- javascript变量,作用域和内存问题
1:ECMAScript所有函数的参数都是按值传递的 function setName(obj){ obj.name="finn"; obj=new Object(); obj.n ...
- ros ddns
ROS5X-6X脚本(10-15分钟执行一次) #DDNS本站帐号 :global ddnsuser "用户名" #DDNS本站密码 :global ddnspass " ...
- 【原】Coursera—Andrew Ng机器学习—Week 11 习题—Photo OCR
[1]机器学习管道 [2]滑动窗口 Answer:C ((200-20)/4)2 = 2025 [3]人工数据 [4]标记数据 Answer:B (10000-1000)*10 /(8*60*60) ...
- C#获取类里面的所有的方法名称
Type trypInfo = typeof(Program);// Program为类名//获得方法名string str = "包括的方法名列表:\r\n"; MethodIn ...
- S初始化生产环境数据
一.将开发机的库文件导出10.10.1.139开发机服务器,桌面上的BAT文件,将数据库表结构和表数据导出来,导到E:\Repository,设置SADMIN密码永不过期BAT文件内容如下: ::导出 ...
- VUE+WebPack前端游戏设计:实现物体的拖拽动态特效