0

I have these two classes:

include/state.hpp :

#include "connection.hpp"
#include <vector>

class state {
    private:
        vector<connection*> conns;
        ...
};

include/connection.hpp :

#include "state.hpp"

class connection {
    private:
        state *next;
        ...
};

And my src/main.cpp :

#include "../include/state.hpp"
#include "../include/connection.hpp"

int main(...) {
    ...
}

The problem is that when I compile I get a lot of errors because the compiler doesnt know what is "state" or "connection" when it needs them. I found a solution doing this:

include/state.hpp :

#include "connection.hpp"
#include <vector>

class connection;

class state {
    private:
        vector<connection*> conns;
        ...
};

include/connection.hpp :

#include "state.hpp"

class state;

class connection {
    private:
        state *next;
        ...
};

Anyway, this is all but clean in my opinion. Should I create other .hpp file with all the classes and include them in the rest of the .hpp like this ? :

include/classes.hpp :

class state;
class connection;
class foo;
...

Is there a better solution ?

Ediolot
  • 501
  • 2
  • 6
  • 19
  • 1
    it looks like design flaw, connection has state, state is basically enum - it does NOT have connections (like in your example), if you want to get all connections by state - create separate class, like connection manager, which will handle map – Iłya Bursov Oct 16 '15 at 20:03
  • Forward-declaring your classes is the normal solution for this. `classes.hpp` is not. – Rostislav Oct 16 '15 at 20:03

0 Answers0