我正在尝试设置一个模拟程序。模拟运行了多个步骤,模拟类应该调用一堆不同类的 ::step(),其中一个是 _experiment 类。
我无法让它工作,因为实验类需要模拟类,而模拟类需要知道实验类是什么,所以它们是循环依赖的。我已经尝试通过使用前向声明来解决它,但是我无法访问前向声明类的方法。那么,前向声明的意义何在?谁能帮我?谢谢!
主文件
int main()
{
_experiment experiment;
}
实验.cpp:
#include "experiment.h"
_experiment::experiment()
{
_simulation simulation;
simulation.experiment = this;
simulation.start();
}
void _experiment::step()
{
//Apply forces to simulation
}
实验.h:
#include "simulation.h"
class _experiment {
public:
void step()
};
模拟.cpp:
#include "simulation.h"
void _simulation::run()
{
//Run simulation for 1000 steps
for(int i = 0; i < 1000; i++)
{
experiment->step() //Calculate forces. Doesnt work (cant use member functions of forward declared classes. How to work around this?
//Calculate motion
}
}
模拟.h:
class _experiment; //Forward declaration
class _simulation {
public:
_experiment* experiment
void run();
};