Force forward declaration, even if class header has already been included

I have a weird problem in C ++ code and I can't figure out what is going on. I know what a forward declaration is, and I know when to use them, etc.

However, in the C ++ project that I have, I am forced to declare a class that has already been declared in the included header. It looks something like this.

windows.h:

#ifndef WINDOWS_HH_ #define WINDOWS_HH_ #include "foo.h" class fooC; // If I don't forward declare here, won't compile!? class WindowC { public: WindowC(); ~WindowC(); public: fooC a; }; #endif 

and then foo.h contains the declaration of fooC

 #ifndef FOO_HH_ #define FOO_HH_ class fooC { public: fooC(); ~fooC(); }; #endif 

Any idea why this might happen? The actual code is part of a large project, and it’s really hard to understand what might be a mistake ... but I am sure, theoretically, that a direct declaration of fooC should not be necessary, is it?

+1
source share
2 answers

A common cause of this effect is the circular relationship between header files.

Does foo.h windows.h (possibly indirectly), creating a circular dependency?

+2
source

In any case, you need the full definition of fooC , because it is included directly (and not by a pointer or reference) in WindowC . As @joval said, this is not necessary. What compiler error do you get? I would look for simple errors such as forgetfulness, including guards or semicolons after class definitions.

0
source

Source: https://habr.com/ru/post/1495040/


All Articles