Question

I'm trying to setup a simulation program. The simulation runs for a number of steps, and the simulation class should call ::step() of a bunch of different classes, one of them is the _experiment class.

I cannot get this to work, because the experiment class needs the simulation class and the simulation class needs to know what an experiment class is, so they are cyclic dependent. I've tried solving it by using a forward declaration, but then I cannot acces methods of the forward declared class. What is the point of forward declaring then? Can anyone help me? Thanks!

main.cpp

int main()
{
    _experiment experiment;
}

experiment.cpp:

#include "experiment.h"

_experiment::experiment()
{
    _simulation simulation;
    simulation.experiment = this;
    simulation.start();
}

void _experiment::step()
{
    //Apply forces to simulation
}

experiment.h:

#include "simulation.h"

class _experiment {
public:
    void step()
};

simulation.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
    }
}

simulation.h:

class _experiment; //Forward declaration

class _simulation {
public:
     _experiment* experiment
     void run();
};
Was it helpful?

Solution

experiment.h does not need to include simulation.h, or forward declare _simulation, since the definition of _experiment doesn't depend on _simulation at all.

You already have a forward declaration or _experiment in simulation.h, which is good, since the definition of _simulation contains a pointer to _experiment, so doesn't need the full definition.

What's missing is the definitions of both classes in the source files. Include both headers from both source files, since they do need the class definitions, and everything should be good.

In general, if you include all the headers you need in source files, and only include a header from another header when you need more than a forward declaration, then you'll mostly avoid circular dependency problems.

You'll also need to add include guards to the headers, to avoid multiple definitions on those occasions when you do need to include headers from other headers.

What is the point of forward declaring then?

It allows you declare that a class exists, without having to declare anything else that the class depends on. You can do several useful things, such as define pointers or references to the class, or declare functions with the class as an argument or return type, with only a forward declaration. You just can't do anything that requires knowledge of the size or members of the class.

OTHER TIPS

You don't have the definition of _experiment in simulation.cpp, include the experiment.h file in the source file and all should work.

Also, the _experiment class doesn't seem to use _simulation in your example, so no need to include simulation.h in experiment.h. Also add include guards to your header files.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top