周期性依赖 - 前向声明不够

时间:2012-10-18 12:22:30

标签: c++ header dependencies header-files

格式化代码:http://pastie.org/5074835

我在Entity类和Component类之间存在循环依赖关系。 我尝试转发类声明,但我必须访问Component的更新方法,这样我就不能。

有没有办法在不重新设计的情况下完成这项工作?

3 个答案:

答案 0 :(得分:3)

您需要#include "Entity.h"中的Component.cpp

答案 1 :(得分:1)

Entity.h中的Component.cppComponent.h中的Entity.cpp包含#include "Entity.h" in是没有问题的。如果将它们包含在头文件中,则会出现循环依赖关系。所以继续{{1}} Component.cpp`。

答案 2 :(得分:0)

用所有方法声明这两个类,然后定义metods?

头:

class B;

class A {
   void method1(B b);
};

class B {
   void method2(A a);
}

源文件:

void A::method1(B b){
   ...
}

void B::method2(A a){
   ...
}
相关问题