C++ 解决由于相互依赖的类而导致的生成错误

C++ 解决由于相互依赖的类而导致的生成错误,c++,C++,我的程序的一部分基本上归结为下面的代码,由于循环依赖性,这些代码将无法编译。我知道之前有关于这个话题的讨论,但之前的解决方案(至少我找不到)都不能解决我的问题: A.h #ifndef A_H #define A_H #include <iostream> #include "B.h" class A { public: typedef struct a_struct { int x; int y; }a_

我的程序的一部分基本上归结为下面的代码,由于循环依赖性,这些代码将无法编译。我知道之前有关于这个话题的讨论,但之前的解决方案(至少我找不到)都不能解决我的问题:

A.h

#ifndef A_H
#define A_H

#include <iostream>

#include "B.h"

class A
{
  public:
    typedef struct a_struct
    {
      int x;
      int y;
    }a_struct_t;

    void print_something() { std::cout << "Hello world" << std::endl; }
  
  private:
    typedef struct another_struct
    {
      int x;
      a_struct_t a_struct;
    }another_struct_t;
    
    friend void B::b_func1();
};
    
#endif
B.cpp

#include "B.h"

void B::b_func()
{ 
  A::another_struct_t another_struct;
  // Do something with another_struct;
} 

A::a_struct_t B::b_func2()
{
  A::a_struct_t a_struct;
  return a_struct;
}
#include "A.h"

int main()
{
  A a_object;
  a_object.print_something();

  return 0;
}
try.cpp

#include "B.h"

void B::b_func()
{ 
  A::another_struct_t another_struct;
  // Do something with another_struct;
} 

A::a_struct_t B::b_func2()
{
  A::a_struct_t a_struct;
  return a_struct;
}
#include "A.h"

int main()
{
  A a_object;
  a_object.print_something();

  return 0;
}
问题是,类A不能在A.h中只使用
void B::B_func1()
而不使用
,包括“B.h”
并完全定义类B,但是类B也不能在
B.h
中使用
A::A_struct
而不使用
;包括“A.h”
并完全定义类
A
我已经尝试在
B.h
中向前声明class
A
,但是我仍然不能在
B.h
中使用
A::A_struct
,因为class
A
是一个不完整的类型

我可以将一个结构移动到类
B
,它可以工作,但它确实属于类
a
。另外,我可以将整个类
B
作为朋友,而不是
#在A.h中包含“B.h”
,这同样有效,但理想情况下,我只想将B::B_func1()作为朋友,而不是整个类
B
。有没有办法让这一切顺利进行?提前感谢,


p.S.-我已经阅读了关于转发声明的内容,该声明是标题为“解决由于类之间的循环依赖性而导致的构建错误”的线程中的首要注释。不幸的是,这在我的情况下不起作用,至少我无法以某种方式应用转发声明来消除
错误:不完整类型“class xxx”的无效使用

由于以下原因,这是不可能的:

  • A.h-不能向前声明B并使用B::B_func1,因为B需要完全定义才能使用B_func1函数
  • B.h–出于同样的原因,不能转发声明A和使用A::A_结构。您也不能向前声明嵌套类/结构,例如::A_struct。()
  • 因此,从最少的工作到最多的工作,我只能想到3种选择:

  • 允许B完全访问A(所有B的朋友)-你已经知道这个了
  • 引入一个助手C,其唯一目的是提供另一个结构
  • 重新设计以避免这种循环依赖
  • 如果使用选项2,保持大部分代码与以前相同,则会出现这种情况:

    A.h

    B.cpp

    C.h

    try.cpp

    #include "A.h"
    
    int main()
    {
      A a_object;
      a_object.print_something();
    
      return 0;
    }
    
    

    为什么在
    a
    的定义中定义了
    a结构?天真地说,似乎最简单的修复方法是将其移动到一个单独的标题(a.h和B.h都将包含该标题),并在不使用
    a::
    的情况下引用它。那么B.h就不需要包含A.h.@zwol实际上
    A
    B
    之间共享了一个结构,并且很早就决定了所有共享的
    struct
    定义都将驻留在
    A
    中。我有几十个
    A
    s和
    B
    s,其中一个
    A
    与一个
    B
    配对。尽管a
    a
    和相应的
    B
    之间存在这种配对关系,并且配对的类共享
    struct
    s,但这两个类完成不同的工作,因此也很早就决定将它们保留为两个不同的类,而不是一个。我想避免单独的标题,因为这样我会有几十个这样的标题。我希望这是有道理的。
    #include "B.h"
    #include "C.h"
    
    void B::b_func1()
    {
      C::another_struct_t another_struct;
      // Do something with another_struct;
    }
    
    A::a_struct_t B::b_func2()
    {
      A::a_struct_t a_struct;
      return a_struct;
    }
    
    #ifndef C_H
    #define C_H
    
    #include "A.h"
    #include "B.h"
    
    class C
    {
      private:
        typedef struct another_struct
        {   
          int x;
          A::a_struct_t a_struct;
        }another_struct_t;
    
       friend void B::b_func1();
    };
    
    #endif
    
    #include "A.h"
    
    int main()
    {
      A a_object;
      a_object.print_something();
    
      return 0;
    }