Solve Error: 'has incomplete type', foward declaration of 'class x'
在C++的OOB编程中,有时候我们会遇到这样的错误Error: 'has incomplete type',forward declaration of 'class x',那么是什么原因引起的这个问题呢,我们首先来看下面这段代码:
// Error: field '_a' has incomplete type 'A'
// forward declaration of 'class A'
class A; class B {
public:
B(A a): _a(a) {}
private:
A _a;
}; class A {
public:
A(B b): _b(b) {}
private:
B _b;
};
在上面这段代码中,类A和类B互相含有对方作为自己的私有成员变量,那么不管谁写在前面,如果不事先声明另一个的话,都会报错找不到定义,那么我们事先声明A就没事了吗,也不是,像上面那样B中声明A的对象还是会出错,因为编译器不知道A的定义,无法生成类A的实例,所以会报错。一种改正方法是把对象实例变成对象指针,如下所示:
// Correct
class A; class B {
public:
B(A *a): _a(a) {}
private:
A *_a;
}; class A {
public:
A(B *b): _b(b) {}
private:
B *_b;
};
还有一种解决方法是引入头文件,这样编译器就知道提前知道类A和类B的结构了,虽然不知道具体的实现,但是互相建对象实例是木有问题的~
Solve Error: 'has incomplete type', foward declaration of 'class x'的更多相关文章
- keil中error: #70: incomplete type is not allowed—解决方法
今天在写程序的时候,想使用sizeof求数组的大小,数组中其他c文件定义,在头文件使用extern uint8_t buff_value[]; 声明 但是keil编译报错,网上查了,发现,需要写成ex ...
- incomplete type is not allowed
keil环境下,报错#70: incomplete type is not allowed,解决 mqtt_conf.h 定义了一个结构体 mqtt_buffer.h #include <std ...
- error: variable '__this_module' has initializer but incomplete type错误解决
版权所有,转载必须说明转自 http://my.csdn.net/weiqing1981127 原创作者:南京邮电大学 通信与信息系统专业 研二 魏清 问题描述:使用SAM9X25 内核版本是2. ...
- error “base class has incomplete type”
error "base class has incomplete type" 如果base.h是你的基类,那么在子类derive中,写成如下形式: class base; clas ...
- error: dereferencing pointer to incomplete type
/******************************************************************************* * error: dereferenc ...
- Render QGraphicsItem on QPixmap: aggregate 'QWidget w' has incomplete type and cannot be defined
Render QGraphicsItem on QPixmap: aggregate 'QWidget w' has incomplete type and cannot be defined #in ...
- std::unique_ptr使用incomplete type的报错分析和解决
Pimpl(Pointer to implementation)很多同学都不陌生,但是从原始指针升级到C++11的独占指针std::unique_ptr时,会遇到一个incomplete type的报 ...
- ASP.NET MVC 提示there was error getting the type的解决方法
在MVC中根据模型类创建控制器时提示there was error getting the type的原因是你新建的这个类模型文件后没有重新生成,先重新生成项目就可以添加控制器了.
- 关于编译报错“dereferencing pointer to incomplete type...
今天同事问了我一个问题,他make的时候报错,“第201行:dereferencing pointer to incomplete type”,我随即查阅了很多资料,也没看出个所以然.最后问题得到了解 ...
随机推荐
- QT基础:QMainWindow学习小结
简述 普通的桌面应用程序有个共同的特性,有菜单栏.工具栏.状态栏.中央窗口等部件.菜单栏其实可以看成是一个窗口,菜单栏中的每一个菜单也可以看成一个窗口,每个部件基本都可以认为是一个窗口.那么这些典型的 ...
- nginx: [emerg] getpwnam(“www”) failed错误
linux 64系统中安装nginx时如果出现错误:nginx: [emerg] getpwnam(“www”) failed in ........解决方法1: 在nginx.conf中 ...
- EXCEPTION:FATAL: UNABLE TO CREATE ‘…GIT/INDEX.LOCK’ FILE EXISTS
FATAL: UNABLE TO CREATE ‘…GIT/INDEX.LOCK’ FILE EXISTS Hi, Today I will share you my other experience ...
- [Converge] Backpropagation Algorithm
Ref: CS231n Winter 2016: Lecture 4: Backpropagation Ref: How to implement a NN:中文翻译版本 Ref: Jacobian矩 ...
- Android重写HorizontalScrollView仿ViewPager效果
Android提供的ViewPager类太复杂,有时候没有必要使用,所以重写一个HorizontalScrollView来实现类似的效果,也可以当做Gallery来用 思路很简单,就是重写onTouc ...
- Dubbo -- 系统学习 笔记 -- 示例 -- 服务分组
Dubbo -- 系统学习 笔记 -- 目录 示例 想完整的运行起来,请参见:快速启动,这里只列出各种场景的配置方式 服务分组 当一个接口有多种实现时,可以用group区分. <dubbo:se ...
- form enctype:"multipart/form-data",method:"post" 提交表单,后台获取不到数据
在解决博问node.js接受参数的时候,发现当form中添加enctype:"multipart/form-data",后台确实获取不到数据,于是跑到百度上查了一下,终于明白为什么 ...
- api 25 PopupWindow会占据整个屏幕
解决方法:if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.N) { // Android 7.x中,PopupWindow高度为match_pa ...
- INSTALL_FAILED_USER_RESTRICTED
我这里出问的问题是在 清单文件中 <provider <mate_data 中少了 android:resource="@xml/filepaths" 加上就好 了
- Python调用7zip命令实现文件批量解压
Python调用7zip命令实现文件批量解压 1.输入压缩文件所在的路径 2.可以在代码中修改解压到的文件路径和所需要解压的类型,列入,解压文件夹下面所有的mp4格式的文件 3.cmd 指的就是Pyt ...