我有这两个类:
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;
...
};
我的src / main.cpp:
#include "../include/state.hpp"
#include "../include/connection.hpp"
int main(...) {
...
}
问题在于,当我编译时,我得到了很多错误,因为编译器在需要时不知道什么是“状态”或“连接”。我找到了一个解决方案:
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;
...
};
无论如何,在我看来,这一切都很干净。我应该创建所有类的其他.hpp文件,并将它们包含在.hpp的其余部分中吗? :
include/classes.hpp :
class state;
class connection;
class foo;
...
有更好的解决方案吗?