Warning: file_get_contents(/data/phpspider/zhask/data//catemap/6/cplusplus/142.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C++ C++;使用多个类、派生类转发声明_C++_Derived Class_Forward Declaration - Fatal编程技术网

C++ C++;使用多个类、派生类转发声明

C++ C++;使用多个类、派生类转发声明,c++,derived-class,forward-declaration,C++,Derived Class,Forward Declaration,目前我有3个课程,设置如下: 世界卫生组织 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 oc

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

世界卫生组织

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){...};
};
我如何使用转发声明,以便可以从World.h访问tobj,就像obj一样,没有错误?我还在每节课开始时使用一次#pragma。我曾尝试将“class World”添加到TextObject.h,并将“class TextObject”添加到World.h,但似乎所有的标准过程都不起作用。有什么建议吗?

#在
.cpp
文件中包含
,而不是在标题中,您可以从那里访问它

您的头include会导致循环依赖,您可以通过转发声明来避免这种情况。

\35;在
.cpp
文件中而不是头中包含
,您可以从那里访问它


你的头include导致循环依赖,你可以通过转发声明来避免这种情况。

如果你试图在World.h中添加
#include“TextObject.h”
,同时添加
类World,会发生什么如果出现错误,它们到底是什么?
#将
包含在.cpp文件中,而不是头文件中,您可以从那里访问它。您的头包含导致循环依赖,您可以通过前向声明避免循环依赖。@Kai修复了它!!这么愚蠢的错误哈哈。如果你想,你可以把它作为一个答案,我会标记正确。太多了。如果您尝试在World.h中添加
#包括“TextObject.h”
,同时添加
类World,会发生什么如果出现错误,它们到底是什么?
#将
包含在.cpp文件中,而不是头文件中,您可以从那里访问它。您的头包含导致循环依赖,您可以通过前向声明避免循环依赖。@Kai修复了它!!这么愚蠢的错误哈哈。如果你想,你可以把它作为一个答案,我会标记正确。太多了。