Hi,
If I have 2 classes, each containing a pointer to the other, is there any way I can have them in two separate files? For example,
Code://Main.cpp #include <stdio.h> #include "Header1.hpp" int main() { Class1 c1; return 0; }Code://Header1.hpp #ifndef HEADER1_HPP #define HEADER1_HPP #include "Header2.hpp" class Class1 { private: Class2 *c; public: int i; int f1() { return c->i; } }; #endif
If I compile this, I get a parse error on the line which defines the pointer to the class, which tells me that the compiler thinks it hasn't been defined yet. The compiler is trying to compile the main file. It includes Header1, which includes Header2. This includes Header1 again but because it's protected, nothing happens. Then it gets to the definition of Class2 in Header2, but Class1 is not yet defined in Header1 (it is still processing the line '#include "Header2.hp"' in Header1), so there is an error.Code://Header2.hpp #ifndef HEADER2_HPP #define HEADER2_HPP #include "Header1.hpp" class Class2 { private: Class1 *c; public: int i; int f2() {return c->i;} }; #endif
Any way to avoid this?



LinkBack URL
About LinkBacks


