C++ c++;使用复杂依赖项进行正确的前向声明 问题

C++ c++;使用复杂依赖项进行正确的前向声明 问题,c++,header-files,forward-declaration,C++,Header Files,Forward Declaration,我收到以下编译错误: 错误:对不完整类型“IRB”的成员访问 这发生在使用类IRB的第一行,即HG.h 问题 我错过了什么?是typedef导致了这些问题吗 在每个头文件上,正如您在下面看到的,我向前声明了我将要使用的类 更重要的是,我应该遵循什么样的过程才能做到这一点 头文件 T.h FH.h 汞柱 CU.h 编辑:开始工作了 在user0042建议之后,我将头从除HG.h之外的所有文件移动到各自的.cc文件中,从而使其工作。 在HG.h中,我保留了转发声明和头文件。这个答案基于我收

我收到以下编译错误:

错误:对不完整类型“IRB”的成员访问

这发生在使用
类IRB
的第一行,即
HG.h

问题
  • 我错过了什么?是typedef导致了这些问题吗

  • 在每个头文件上,正如您在下面看到的,我向前声明了我将要使用的类

  • 更重要的是,我应该遵循什么样的过程才能做到这一点

头文件 T.h FH.h 汞柱 CU.h 编辑:开始工作了 在user0042建议之后,我将头从除
HG.h
之外的所有文件移动到各自的
.cc
文件中,从而使其工作。
HG.h
中,我保留了转发声明和头文件。

这个答案基于我收到的评论

头文件中的函数声明可能使用了我们不想包含的类,但这可能会导致循环依赖

因此,解决方案是:

  • 要将头文件中提到的类的
    #include
    ”移动到源文件中
  • 在头文件中执行类的
    转发声明
这可以对标题中未使用的任何类执行。如果我们使用头文件,如下面的示例
HG.h
,我们应该在头文件中保留
#include

有关示例的解决方案 T.h FH.h 汞柱 CU.h
如果有转发声明,为什么要保留标题?这不是违背了它的目的吗?
typedef。。。IRB_基地
<代码>IRB类:IRB_基数-注释资本化。(如果你想说这只是问题中的一个输入错误,而这与你的真实例子不符,那么就发布一条。)@BoBTFish更新了答案。对不起@user0042是的,但是如果我删除它们,*.cc文件会抱怨。您可以从其他标题中删除它们,但要包含在.cc文件中。然而,这个愚蠢的问题——IRBBase从哪里来?您正在实例化的模板类是否在T.h中声明?
typedef IRBBase__<true, true> IRB_BASE;  // typedef a template instantiation
#include "T.h"
#include "FH.h"
class FH; // FWD DCLR
class IRB : IRB_BASE {..};
#include "T.h"
#include "IRB.h"
#include "HG.h"
class IRB;  // FWD DCLR
class HG;   // FWD DCLR
class FH {..};
#include "T.h"
#include "FH.h"
#include "IRB.h"
#include "CU.h"
class FH;   // FWD DCLR
class IRB;  // FWD DCLR
class CU;   // FWD DCLR
class HG {..};
#include "T.h"
#include "IRB.h"
#include "FH.h"
class IRB;
class FH;
class CU {..};
typedef IRBBase__<true, true> IRB_BASE;  // typedef a template instantiation
class FH; // FWD DCLR
class IRB : IRB_BASE {..};
class IRB;  // FWD DCLR
class HG;   // FWD DCLR
class FH {..};
#include "T.h"
#include "FH.h"
#include "IRB.h"
#include "CU.h"
class FH;   // FWD DCLR
class IRB;  // FWD DCLR
class CU;   // FWD DCLR
class HG {..
   // some of the classes are used here, because some functions
   // were too small and kept them in the header file (defined the function)
   // if the same had happened with another of the headers, causing a
   // cyclic include dependency, then I should have converted
   // some of the function definitions into declarations, and move the
   // implementation of the file in the source file (.cc)
};
class IRB;
class FH;
class CU {..};