C ++不能建立处理参照“父”对象 - 圆形包括或未定义类型的错误 [英] C++ cannot establish handle reference to 'parent' object -- circular includes or undefined type errors

查看:124
本文介绍了C ++不能建立处理参照“父”对象 - 圆形包括或未定义类型的错误的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我期待有一个分层的类结构,其中控制器的水平父类负责创建/引导一批'孩子'类。父类应该能够引用每次直接创造的孩子,每个孩子应该能够引用它的父(和,假设这孩子是不是也多类的父,只有其父)直接。这使得兄弟姐妹通过父引用。我发现这种模式是在JIT编译语言如Java和C#有用的,但C ++ presents一个独特的问题...

I'm looking to have a hierarchical class structure in which a level of controller 'parent' classes are responsible for creating/directing a number of 'child' classes. The parent class should be able to reference each child it creates directly, and each child should be able to reference its parent (and, assuming this child is not also a parent of more classes, only its parent) directly. This allows for the referencing of siblings through the parent. I've found this paradigm to be useful in JIT compilation languages like Java and C#, but C++ presents a unique problem...

我的第一个实施这种模式的尝试如下:

My first attempt to implement this paradigm was as follows:

TreeRoot.h父类

Parent Class TreeRoot.h

#ifndef __CCPP_SCENE_H__
#define __CCPP_SCENE_H__
#include "ChildA.h"
#include "ChildB.h"

class TreeRoot : 
{
private:


ChildA* a;
ChildB* b;

public:

//member getters
ChildA* getA();
ChildB* getB();
};

#endif // __CCPP_SCENE_H__

儿童类ChildA.h

Child class ChildA.h

#ifndef CHILDA_H_
#define CHILDA_H_


#include "TreeRoot.h"


class ChildA
{

private:
TreeRoot* rootScene;

public:
ChildA(TreeRoot*);
~ChildA(void);

TreeRoot* getRootScene();
void setRootScene(TreeRoot*);

};

#endif /*CHILDA_H_*/

儿童类ChildB.h

Child class ChildB.h

#ifndef CHILDB_H_
#define CHILDB_H_


#include "TreeRoot.h"


class ChildB
{

private:
TreeRoot* rootScene;

public:
ChildB(TreeRoot*);
~ChildB(void);

TreeRoot* getRootScene();
void setRootScene(TreeRoot*);

};

#endif /*CHILDB_H_*/

现在当然因为这不会编译的循环包括:(TreeRoot.h包括ChildA.h和ChildB.h,这既包括TreeRoot.h等),所以我试图用向前声明代替:

Now of course that wouldn't compile because of the circular include (TreeRoot.h includes ChildA.h and ChildB.h, which both include TreeRoot.h etc) So I tried using forward declaration instead:

TreeRoot.h父类

Parent Class TreeRoot.h

#ifndef __CCPP_SCENE_H__
#define __CCPP_SCENE_H__
#include "ChildA.h"
#include "ChildB.h"

class TreeRoot : 
{
private:


ChildA* a;
ChildB* b;

public:

//member getters
ChildA* getA();
ChildB* getB();
};

#endif // __CCPP_SCENE_H__

儿童类ChildA.h

Child class ChildA.h

#ifndef CHILDA_H_
#define CHILDA_H_


//#include "TreeRoot.h" //can't use; circular include!
class TreeRoot;

class ChildA
{

private:
TreeRoot* rootScene;

public:
ChildA(TreeRoot*);
~ChildA(void);

TreeRoot* getRootScene();
void setRootScene(TreeRoot*);

};

#endif /*CHILDA_H_*/

儿童类ChildB.h

Child class ChildB.h

#ifndef CHILDB_H_
#define CHILDB_H_


//#include "TreeRoot.h" //can't use; circular include!
class TreeRoot;



class ChildB
{

private:
TreeRoot* rootScene;

public:
ChildB(TreeRoot*);
~ChildB(void);

TreeRoot* getRootScene();
void setRootScene(TreeRoot*);

};

#endif /*CHILDB_H_*/

这几乎实现在工作,我可以成功地发送广播消息到子对象,并从子对​​象的父类进行回调如下:

that implementation almost works in that I can successfully broadcast messages to the child objects and perform callbacks from the child objects to the parent class as follows:

TreeRoot.cpp

TreeRoot.cpp

...
a->someChildMethod();
a->getRootScene()->someParentMethod();

然而,当我尝试以下方法:

However when I try the following:

ChildA.cpp

ChildA.cpp

...
rootScene->someParentMethod(); //ERROR C2027: use of undefined type TreeRoot

我得到一个未定义的类型错误。这使得因为使用向前声明如上不告知什么TreeRoot实际上是编译器感。接下来的问题是如何能够使呼叫从子对象像rootScene-> someParentMethod()调用上面?也许通过模板一些使用泛型类型将使编译快乐,并提供我正在寻找的功能?

I get an undefined type error. This makes sense since using forward declaration as above doesn't inform the compiler of what TreeRoot actually is. The question then is how can I enable calls from the child objects like the rootScene->someParentMethod() call above? Perhaps some use of generic types via templates would make the compiler happy and provide the functionality I'm looking for?

谢谢,
CCJ

thanks, CCJ

推荐答案

使用在所有的 .H 文件的向前声明。你可以这样做,因为你只存储指针作为类成员,所以你并不需要完整的类声明。

Use a forward declaration in all your .h files. You can do this since you're only storing pointers as class members so you don't need the full class declaration.

然后,在你所有相应的的.cpp 文件,的#include 您需要的类的头文件。

Then, in all your corresponding .cpp files, #include the header files for the classes you need.

所以,在 TreeRoot.h 你向前声明 ChildA ChildB 。在 TreeRoot.cpp ,您的#include ChildA.h ChildB.h

So, in TreeRoot.h you forward declare ChildA and ChildB. In TreeRoot.cpp, you #include ChildA.h and ChildB.h.

冲洗和重复你的2其他类。

Rinse and repeat for your 2 other classes.

注意到,这将解决当前的问题,但这样的设计似乎充其量片状。

Take note that this will solve your current problem, but this design seems flaky at best.

这篇关于C ++不能建立处理参照“父”对象 - 圆形包括或未定义类型的错误的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆