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'

时间: 2024-08-27 03:30:23

Solve Error: 'has incomplete type', foward declaration of 'class x'的相关文章

incomplete type is not allowed

keil环境下,报错#70: incomplete type is not allowed,解决 mqtt_conf.h 定义了一个结构体 mqtt_buffer.h #include <stdint.h>#include "mqtt.h" 定义了一个结构体 struct MqttBuffer{ struct MqttExtent *first_ext; struct MqttExtent *last_ext; uint32_t available_bytes; char

Redhat编译php-5.2.9出现error dereferencing pointer to incomplete type

错误1: /usr/src/php-5.2.9/ext/dom/node.c:In function 'dom_canonicalization': /usr/src/php-5.2.9/ext/dom/node.c:1950:21: error: dereferencingpointer to incomplete type ret = buf->buffer->use; ^ Infile included from /usr/src/php-5.2.9/main/php.h:38:0, f

error: invalid use of incomplete type

一般出现这种情况都是没有将用到的头文件包含进来 我的情况是在头文件中定义了一个QMenu的指针,在源文件中使用menuBar()函数来返回一个menu指针.我在源文件中包含了文件<QtGui>出现这个错误, .h QMenu *fileMenu; .cpp fileMenu = menuBar()->addMenu(tr("&File")); 解决办法是:在源文件中添加对<QMenuBar>的包含即可. error: invalid use of

解决编译错误:dereferencing pointer to incomplete type 的办法

在使用c语言写程序时,可能遇到错误:error :dereferencing pointer to incomplete type.其实,这个错误是指针指向的结构体类型没有定义. 原因可能有很多,但最多情况可能下面两种: 1,使用库函数或内核等提供的结构体时,没有包含相应的头文件.解决方法很简单,就是包含对应头文件就ok了. 2,若是自己定义的结构体,而且这个结构体恰恰是定义在.c文件中.在这种情况下,在其他.c文件中使用该结构体指针时,也会出现类似错误. 就第二种情况,笔者找到两种解决方法:

Render QGraphicsItem on QPixmap: aggregate &#39;QWidget w&#39; has incomplete type and cannot be defined

Render QGraphicsItem on QPixmap: aggregate 'QWidget w' has incomplete type and cannot be defined #include <QWidget> and #include <QStyleOptionGraphicsItem> should fix the compile error you are getting. 常见错误: 头文件未包含 Render QGraphicsItem on QPix

gcc: dereferencing pointer to incomplete type错误

/*********************************************************************  * Author  : Samson  * Date    : 07/27/2014  * Test platform:  *              Mint 15  *              GNU bash, version 4.2.45  * *************************************************

关于编译报错“dereferencing pointer to incomplete type...

今天同事问了我一个问题,他make的时候报错,“第201行:dereferencing pointer to incomplete type”,我随即查阅了很多资料,也没看出个所以然.最后问题得到了解决,也懂得了原理,遂记录一下. 他的问题具体是这样. ? 1 2 3 4 5 6 #include <netinet/ip_icmp.h> ... struct icmp* aaa;     aaa = (struct icmp*)malloc(sizeof(struct icmp)); //假设

Solve error: &#39;class vtkImageActor&#39; has no member named &#39;SetInput&#39;

Replacement of SetInput() with SetInputData() and SetInputConnection() someFilter->SetInput(someReader->GetOutput()); // Outdated // Replace to the following: someFilter->SetInputConnection(someReader->GetOutputPort()); someFilter->SetInput

scala 2.11报错error: not found: type Application

FROM: http://j-q-j.org/scala/scala-2-11-application-error.html 这两天学习scala,官网下载的最新版本2.11,书用的是<Programming in scala>,看到类和对象,这一章最后一段代码 1 2 3 4 5 import ChecksumAccumulator.calculate object FallWinterSpringSummer extends Application {   for (season <