Reputation: 15
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.
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;
Upvotes: 1
Views: 298
Reputation: 385204
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.
Upvotes: 2