简体   繁体   中英

C++ cannot establish handle reference to 'parent' object — circular includes or undefined type errors

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:

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__

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_*/

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_*/

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:

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__

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_*/

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

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

However when I try the following:

ChildA.cpp

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

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?

thanks, CCJ

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.

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

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

Rinse and repeat for your 2 other classes.

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

You could try including the 'TreeRoot.h' in the ChildA and ChildB files. I would also suggest using Polymorphism and create a parent class, which A and B inherit from, for shared behaviour.

这不涉及摆弄头文件,但是我的建议是:要么将所有节点都设为同一类(启用更大的灵活性[如果您决定将一棵树做成另一棵树的子树,那该怎么办?将根节点的类从第一棵树更改为子类],并且至少在我看来,这更有意义/似乎更优雅/将减少您必须编写的代码量和/或简化代码),或者对父级和子级节点类都使用超类(如ATaylor建议的那样),尽管我认为,只有当您的父级和子级节点具有很多不同的功能(而不是它们形成节点所需的功能)时,这才是更好的解决方案树结构。

在ChildA.cpp文件中,您必须将父标头包括为

#include "TreeRoot.h"

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM