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配置 ...
随机推荐
- ubuntu安装Percona Server
Percona Server是mysql数据库的一个衍生版本,在性能,稳定性和可管理性上都有很大的提升,目前淘宝的mysql数据库就是基于Percona Server,请原谅我这么喜欢研究淘宝的技术. ...
- python 常用模块 time random os模块 sys模块 json & pickle shelve模块 xml模块 configparser hashlib subprocess logging re正则
python 常用模块 time random os模块 sys模块 json & pickle shelve模块 xml模块 configparser hashlib subprocess ...
- VRRP概述
随着Internet的发展,人们对网络的可靠性的要求越来越高.对于局域网用户来说,能够时刻与外部网络保持联系是非常重要的. 通常情况下,内部网络中的所有主机都设置一条相同的缺省路由,指向出口网关(即图 ...
- spring中 Bean的装配 Bean后处理器
- 封装basedao及动态创建新类型的java数组
package com.huawei.base; import java.io.Serializable;import java.lang.reflect.Array;import java.lang ...
- call和callvirt
call以非虚方式调用虚函数. callvirt以虚方式调用虚函数,调用的时候会判断真正引用的对象,调用该类型的
- fedora 16 yum yuan
暑假买了几本Linux的书一直放在书架上没看,周末闲着没事就拿起本<LinuxC从入门到精通>看了起来,初学Linux首先要做的便是在电脑上安装Linux系统.于是按书上的要求下载了Fed ...
- flume 转
http://blog.csdn.net/xiao_jun_0820/article/details/38111305
- 494. Target Sum 添加标点符号求和
[抄题]: You are given a list of non-negative integers, a1, a2, ..., an, and a target, S. Now you have ...
- 530.Minimum Absolute Difference in BST 二叉搜索树中的最小差的绝对值
[抄题]: Given a binary search tree with non-negative values, find the minimum absolute difference betw ...