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配置 ...
随机推荐
- 「小程序JAVA实战」小程序视图之细说wx:key列表高级特性(16)
转自:https://idig8.com/2018/08/09/xiaochengxu-chuji-16/ wx:key的高级特性.这个很重要,因为在app上经常有上拉,下拉加载,我们如果不使用这个特 ...
- Spring Test 整合 JUnit 4 使用总结
转自:https://blog.csdn.net/hgffhh/article/details/83712924 这两天做Web开发,发现通过spring进行对象管理之后,做测试变得复杂了.因为所有的 ...
- URLDownloadToFileW
[ilink32 Error] Error: Unresolved external 'URLDownloadToFileW' referenced from D:\USERS\PUBLIC\DOCU ...
- C#模板的效率问题
1,有拆装箱的情景时,可使用模板方式避免拆装箱,这时候使用模板比不使用效率要高很多. 2,无拆装箱的操作时,全部是值传递,使用模板会比使用基本类型慢一半
- 读取resource下文件
ArrayList<PatrolOper> patrolOpers = new ArrayList<>(); String jsonData = null; File json ...
- REST介绍与REST在PHP中的应用
当HTTP被发明出来的时候,其实REST就已经存在了.可惜这么多年来,WEB开发模式却越来越背离HTTP的本质,舍本逐末的追求起RPC之类的东西.此时REST重新回到人们的视线里,无疑让大家开始反思过 ...
- NPC问题及其解决方法(回溯法、动态规划、贪心法、深度优先遍历)
NP问题(Non-deterministic Polynomial ):多项式复杂程度的非确定性问题,这些问题无法根据公式直接地计算出来.比如,找大质数的问题(有没有一个公式,你一套公式,就可以一步步 ...
- Oracle-属性查询
1. 查询表的部分字段属性 select t.*, c.comments from user_tab_columns t, user_col_comments c where t.table_name ...
- c# 类中使用ResolveUrl
类中使用ResolveUrl 1>获取当前page然后调用ResolveUrl System.Web.UI.Page page = HttpContext.Current.CurrentHand ...
- 快速搭建Wordpress
1. 下载:ZentaoPMS作为Mysql Apach Php的基础环境: 2. 下载:Wordpress安装包: 3. 将Wordpress解压,放置于ZentaoPMS的Xampp的htdocs ...