I am having an issue in my project when trying to include my Game.h file in certain files of the project. I get a lot of errors, all of which say the following:
error C2143: syntax error : missing ';' before '*'
error C4430: missing type specifier - int assumed. Note: C++ does not support default-int
These errors get associated with the Game.h file and Level.h files.
I figured out that it is an issue with having with including the same header file in multiple files and it seems to confuse the compiler. I was able to fix the error by using forward declarations for all the lines that had a missing type error. I'm assuming my code has some fundamental issues on when I should be including a header or not.
After doing some reading online it seems that: if I can get away with a forward declaration I should use it. If I need definitions, I should include these header's within the CPP file instead of the header file. Is that a good practice?
> if I can get away with a forward declaration I should use it.
> If I need definitions, I should include these header's within the CPP file instead of the header file.
> Is that a good practice?
Each header file should include a minimal but complete set of headers needed to compile it's own contents. If one header has a genuine dependency on the contents of another header, it should include that header.
So, if the contents of A.h make reference to type B, but don't require information in B.h, you should forward declare B.
If, however, the contents of A.h need information about the contents of B.h, then A.h should include B.h.
If you're working on a large project, writing your header files in such a way as to reduce dependencies on other headers becomes important!
EDIT: Having said all that, there should never be a problem including the same header file in different cpp files, or even including the same header twice in the same cpp file. If that's happening, then you've done something wrong.
Are you using multiple inclusion guards (or #pragma once )?
Are you defining globals with external linkage in header files, resulting in multiple definitions of the same thing?
Having said all that, there should never be a problem including the same header file in different cpp files, or even including the same header twice in the same cpp file. If that's happening, then you've done something wrong.
I think the issue I was having was due to the fact that I was including the header in other header files instead of the CPP file. For some reason, this somehow tricked the compiler into thinking it did not know anything about a few of my classes. I'm still not 100% sure why that happens....
Are you using multiple inclusion guards (or #pragma once )?
Yes
Are you defining globals with external linkage in header files, resulting in multiple definitions of the same thing?
I do have some globals defined, one of which is in the "Game.h" file I was having trouble with. I'm not sure it was tripping over that, at least it wasn't obvious based on my compiler errors. Again, once I added some class forward declarations to my game class, the errors went away, which suggests the compiler didn't think it knew what my classes were.
I think the issue I was having was due to the fact that I was including the header in other header files instead of the CPP file. For some reason, this somehow tricked the compiler into thinking it did not know anything about a few of my classes. I'm still not 100% sure why that happens....
Unless you're doing something really bizzarre - say, using the same symbol for inclusion guards in more than one header (an easy enough copy-and-paste mistake to make), I don't see how that could be the case. Was your include statement inside a namespace, perhaps, causing the classes to be defined in the wrong namespace?
I do have some globals defined, one of which is in the "Game.h" file I was having trouble with. I'm not sure it was tripping over that, at least it wasn't obvious based on my compiler errors.
If that was the issue, the linker would have given you errors about multiple definitions of symbols.