[C ++前向声明具有多个类,派生类

问题描述 投票:0回答:1

目前我有3个课程,设置如下:

World.h

include "WorldObject.h"
class WorldObject;
class TextObject; // when this is added, compiles fine, but tobj is incomplete when accessed 
//(world->tobj->function()).  if #include "TextObject.h" is added, numerous errors occur within TextObject.h
class World { 
public:
      WorldObject* wobj; // works fine
      TextObject* tobj; //trying to get this to be functional
};

WorldObject.h

#include "World.h"
class World;
class WorldObject {
public:
      WorldObject(World* world){...} // works fine, world can be accessed from world objects
};

TextObject.h

#include "WorldObject.h"
#include "World.h"
class TextObject : WorldObject {
public:
      TextObject(World* world) : WorldObject(w){...};
};

如何使用前向声明,以便可以像obj一样从World.h访问tobj,并且没有错误?在每个课程的开始,我也使用#pragma。我试图将“类世界”添加到TextObject.h,将“类TextObject”添加到World.h,但是看似标准的程序都没有起作用。有什么建议吗?

c++ derived-class forward-declaration
1个回答
0
投票
您的标头中的内容引起循环依赖,您可以通过前向声明避免循环依赖。
© www.soinside.com 2019 - 2024. All rights reserved.