1

The program concerns a railway network simulation programmed in C++. The program consists of five different classes, namely the Controller (keeps track of time and schedules new events), Network (Grabs different parts of the model together), Train, Station and Trip.

Trip is basically a node with a station as begin point and as end point. Train depends on trips, because the train keeps track of where it has to go. Station has a vector of trains. The dependencies look as follows in a figure.

Graph

I have looked at the solution proposed in related questions, but those solutions only concerned a simple A -> B, B -> A relationship. Below are some relevant snippets of the code.

Edit: First, station is compiled. This gives no problems, but when I switch to trip then it gives errors of the following form:

g++  --std=c++14 -Wall -g -O2 -c -o tmp/o/3train1.o train/train1.cc
In file included from train/../trip/trip.h:4:0,
                 from train/train.h:6,
                 from train/train.ih:1,
                 from train/train1.cc:1:
train/../trip/../station/station.h:13:14: error: ‘Train’ was not declared in this scope
  std::vector<Train*> d_trains;

trip.h

#include "../station/station.h"
#include <chrono>
class Station; // forward declaration
class Trip
{
    Station &d_beginStation;
    Station &d_endStation;
    std::chrono::minutes d_durationTrip;

train.h

#include "../trip/trip.h"

class Train
{
    typedef std::chrono::system_clock::time_point Timepoint;

    size_t d_id;
    std::list<Trip*> &d_route;

station.h

#include "../train/train.h"

class Station
{
    std::string d_name;
    std::chrono::minutes d_waitTimeStation;
    std::vector<Train*> d_trains;

network.h

#include "../train/train.h"
#include "../station/station.h"
#include "../trip/trip.h"

class Network
{
    typedef std::list<Trip> Route;

    std::list<Train *> d_trains;
    std::list<Station> d_stations;
    std::list<Route> d_routes;
bytefast
  • 15
  • 3
  • So what is the actual problem? Is this not working? If not what error are you getting? – NathanOliver Apr 08 '16 at 14:20
  • 1
    You may want to look at Forward References, see: http://stackoverflow.com/questions/4757565/c-forward-declaration – Robinson Apr 08 '16 at 14:23
  • I edited the question to also show the actual error messages. I looked at it, but I only can grasp my head around this with just two classes. When this problem is extended to more classes I do not see how this will work. – bytefast Apr 08 '16 at 14:25
  • @Robinson: "Forward declarations". References are something else. – Lightness Races in Orbit Apr 08 '16 at 14:28

1 Answers1

2

Use more forward declarations to loosen all those header inter-dependencies.

You've already forward declared Station when you have a Station&; now forward declare Train when you have a std::vector<Train*>, and so on.

Then you can remove a ton of includes and you don't have cyclic dependency problems any more.

Lightness Races in Orbit
  • 378,754
  • 76
  • 643
  • 1,055
  • Thanks! This solved the problem. With forward declaration how do the classes that use forwarding now know about the implementation of this class. Is this in the linking phase? – bytefast Apr 08 '16 at 14:50
  • @bytefast: No it's still at the compilation phase; your .cpp files will have to `#include` those headers. But since .cpp files don't interact with each other during compilation, there's no conflict there. And the linker (which _does_ handle all the source files together) is clever enough to resolve inter-dependencies. So you're basically "delaying" the circular dependency problem until it's not actually a problem. – Lightness Races in Orbit Apr 08 '16 at 14:52