connector.hpp
#ifndef __CONNECTOR_HPP_
#define __CONNECTOR_HPP_
#include <a/b/c/connection.hpp>
namespace a {
namespace b {
namespace c {
class connector {
public:
explicit connector(const int port);
void run();
void stop();
};
}
}
}
#endif
connection.hpp
#ifndef __CONNECTION_HPP_
#define __CONNECTION_HPP_
#include <a/b/c/connector.hpp>
namespace a {
namespace b {
namespace c {
class connection {
private:
connector owner_; //line 42
};
}
}
}
#endif
These simple classes have some trivial (empty) implementations in cpp files.
VS2012 says this when I try to compile this:
Error 1 error C2146: syntax error : missing ‘;’ before identifier ‘owner_’ c:\a\b\c\connection.hpp 42 1 test
Error 2 error C4430: missing type specifier – int assumed. Note: C++ does not support default-int c:\a\b\c\connection.hpp 42 1 test
The thing is that VS editor sees no problems – no red underlines etc.
Include paths are set correctly, I’ve added $(SolutionDir)\..\ to includes so that I can use full paths like a/b/c instead of relative ones.
Still, this doesnt compile.. but it did once.
Q: WHY?
Oftentimes those circular
#includedirectives are deadly. That’s certainly what’s happening here. Suppose you#include "connector.hpp"in some other file. What’s going to happen is that connector.hpp will#include "connection.hpp". This file will in turn#include "connector.hpp", but this is now a no-op because the include guards for connector.hpp are already active. What you end up with isclass connectionbeing defined beforeclass connector. That’s a problem because a data member inclass connectionis an instance ofclass connector.Why is connector.hpp including connection.hpp?