C++ 我需要将两个头文件包含在一起,而不使用前向声明cause get";“不完整类型”;错误

C++ 我需要将两个头文件包含在一起,而不使用前向声明cause get";“不完整类型”;错误,c++,class,oop,header,forward-declaration,C++,Class,Oop,Header,Forward Declaration,我需要包括两个头文件,以对方,但我有麻烦这样做。除了使用转发声明和模板之外,还有其他方法可以做到这一点吗?或者我不允许在C++中做?< /P> 以下是我想做的: // A.hpp file #ifndef H_A_H #define H_A_H #include "B.hpp" class A { private: vector<B*> b; public: void function() { // using methods of B } }; #endif /

我需要包括两个头文件,以对方,但我有麻烦这样做。除了使用转发声明和模板之外,还有其他方法可以做到这一点吗?或者我不允许在C++中做?< /P> 以下是我想做的:

// A.hpp file
#ifndef H_A_H
#define H_A_H
#include "B.hpp"
class A {
private:
  vector<B*> b;
public:
  void function() {
  // using methods of B
  }
};
#endif


// B.hpp file
#ifndef H_B_H
#define H_B_H
#include "A.hpp"
class B {
private:
  vector<A*> a;
public:
  void function() {
  // using methods of A
  }
};
#endif
//A.hpp文件
#如果没有
#定义H_A_H
#包括“B.hpp”
甲级{
私人:
载体b;
公众:
空函数(){
//使用B
}
};
#恩迪夫
//B.hpp文件
#如果没有
#定义H_B_H
#包括“A.hpp”
B类{
私人:
载体a;
公众:
空函数(){
//使用
}
};
#恩迪夫

您具有循环依赖关系。这说明了如何通过转发声明来处理它们

这也处理循环依赖关系

如果您100%不想使用前向声明,那么您可以在不同的类中拆分逻辑并使用组合

// SomeLogic.h
class SomeLogic
{
};

// A.h
#include "SomeLogic.h"
class A
{
    SomeLogic someLogic;
};

// B.h
#include "SomeLogic.h"
class B
{
    SomeLogic someLogic;
};

你有一个循环依赖。这说明了如何通过转发声明来处理它们

这也处理循环依赖关系

如果您100%不想使用前向声明,那么您可以在不同的类中拆分逻辑并使用组合

// SomeLogic.h
class SomeLogic
{
};

// A.h
#include "SomeLogic.h"
class A
{
    SomeLogic someLogic;
};

// B.h
#include "SomeLogic.h"
class B
{
    SomeLogic someLogic;
};

不能相互包含两个头文件。其中一个文件中应该有转发声明,并且必须将函数定义推送到.cpp文件中,您可以在其中包含头文件

// HeaderA.h file
#ifndef H_A_H
#define H_A_H
#include "HeaderB.h"
class A {
private:
  int b;
public:
  void function() {
  // using methods of B
      B b;
      b.function();
  }
};
#endif

// HeaderB.h file
#ifndef H_B_H
#define H_B_H

class A;

class B {
private:
  int a;
public:
  void function();
};
#endif

// Main.cpp
#include "HeaderA.h"
#include "HeaderB.h"

 void B::function()
 {
  // using methods of A
      A a;
      a.function();
 }

int _tmain(int argc, _TCHAR* argv[])
{
    return 0;
}

不能相互包含两个头文件。其中一个文件中应该有转发声明,并且必须将函数定义推送到.cpp文件中,您可以在其中包含头文件

// HeaderA.h file
#ifndef H_A_H
#define H_A_H
#include "HeaderB.h"
class A {
private:
  int b;
public:
  void function() {
  // using methods of B
      B b;
      b.function();
  }
};
#endif

// HeaderB.h file
#ifndef H_B_H
#define H_B_H

class A;

class B {
private:
  int a;
public:
  void function();
};
#endif

// Main.cpp
#include "HeaderA.h"
#include "HeaderB.h"

 void B::function()
 {
  // using methods of A
      A a;
      a.function();
 }

int _tmain(int argc, _TCHAR* argv[])
{
    return 0;
}

1) 在声明
B
之前,是什么阻止您向前声明
A
,反之亦然?2) 您应该在.cpp文件中定义方法,其中包括这两个标题。您还可以创建一个
C
类,该类同时对
a
B
进行操作,以便
a
B
不需要“了解”彼此。。或者对你的
A
->
B
映射使用
std::map
,然后对你的
B
->
A
映射使用
std::map
,去掉类中的
向量….@AlgirdasPreidžius你提到的第二点正是我的另一个问题。每当我在cpp文件中定义方法时,都会出现“多重定义…”错误。@txtechhelp谢谢您,但我认为这对我的设计不是一个好主意。@shirazy,那么,您不应该:1)将said.cpp文件包含到其他文件中,或2)在两个标题中定义它,和.cpp文件。1)是什么阻止您在声明
B
之前向前声明
a
,反之亦然?2) 您应该在.cpp文件中定义方法,其中包括这两个标题。您还可以创建一个
C
类,该类同时对
a
B
进行操作,以便
a
B
不需要“了解”彼此。。或者对你的
A
->
B
映射使用
std::map
,然后对你的
B
->
A
映射使用
std::map
,去掉类中的
向量….@AlgirdasPreidžius你提到的第二点正是我的另一个问题。每当我在cpp文件中定义方法时,都会出现“多重定义…”错误。@txtechhelp谢谢您,但我认为这对我的设计不是一个好主意。@shirazy那么,您不应该:1)将said.cpp文件包含到其他文件中,或者2)在头文件和.cpp文件中都定义它。