Welcome!

Join our community of MMO enthusiasts and game developers! By registering, you'll gain access to discussions on the latest developments in MMO server files and collaborate with like-minded individuals. Join us today and unlock the potential of MMO server development!

Join Today!

Web [C++] Easy fix to avoid these header problems

Elite Diviner
Joined
Mar 24, 2015
Messages
426
Reaction score
416
Hello ragezone,
so I technically know how headers work, you put the "layout" in the header and then the implementation in the complementary .cpp file. But still the more my client project grows, the more trouble I seem to have with #include and whatnot.
Currently when I add a new class, there will be like a 50% chance that my project will not compile anymore. The compiler will then keep giving me misleading errors like: "missing type specifier" (but the type specifier in question is a class that I added last week and that worked fine before).
Those errors can take me up to an hour to debug and the solution is usually to swap the positions of two header's #include or similiar nonsense.

Here are the errors I usually get (none of which make any sense if I look at the code):
- Missing type specifier
- Missing ";" before "packet_creator" (where packet_creator is a variable name)
- Type X was not defined in this scope (it was)

Visual Studio by default adds "#pragma once" in each header file, which supposedly fixes #include collisions. But apparently I still manage to ruin my project every time.
I know that this question is very general, but googling it didn't really give me anything. Questions about it on stackoverflow are usually by people who actually do have these errors in their code.

Any tips for this? How do the c++ users here organize their code to avoid these problems? Help is appreciated, thanks.
 
Skilled Illusionist
Joined
Aug 17, 2011
Messages
360
Reaction score
88


Hello ragezone,
so I technically know how headers work, you put the "layout" in the header and then the implementation in the complementary .cpp file. But still the more my client project grows, the more trouble I seem to have with #include and whatnot.
Currently when I add a new class, there will be like a 50% chance that my project will not compile anymore. The compiler will then keep giving me misleading errors like: "missing type specifier" (but the type specifier in question is a class that I added last week and that worked fine before).
Those errors can take me up to an hour to debug and the solution is usually to swap the positions of two header's #include or similiar nonsense.

Here are the errors I usually get (none of which make any sense if I look at the code):
- Missing type specifier
- Missing ";" before "packet_creator" (where packet_creator is a variable name)
- Type X was not defined in this scope (it was)

Visual Studio by default adds "#pragma once" in each header file, which supposedly fixes #include collisions. But apparently I still manage to ruin my project every time.
I know that this question is very general, but googling it didn't really give me anything. Questions about it on stackoverflow are usually by people who actually do have these errors in their code.

Any tips for this? How do the c++ users here organize their code to avoid these problems? Help is appreciated, thanks.

I don't code in C++ but you should make a separate class that includes all of the files you want, and then only include that one class to your project. I don't really have these problems on vb.net/c# but then again i usually don't call many imports.
 
Upvote 0
Joined
Apr 5, 2008
Messages
663
Reaction score
537
The way I do it is every source file (.cpp) has a corresponding header (.hpp). In the header I put declarations for every type and function that will be used from other headers or source files. I also put #pragma once at the beginning of each header. Each source file will #include its respective header first before any other #include s, and then at the top of the header I will put any #include s that are necessary for the header itself while anything only the source file needs goes in the source file after the inclusion of its respective header. I try to make sure that the graph of headers including each other is a directed acylic graph, but in the rare case that I absolutely need two headers to be able to depend on each other, I will just add a minimal amount of forward declarations to one of them such that it doesn't need to include the other and so the acylic property is maintained.

Anyway, I'm so glad I moved to Rust so I don't have to deal with headers or the order of anything. The error messages are actually helpful as well, and since it's just as fast and offers me the same control that C++ does, I definitely don't miss C++.
 
Upvote 0
Elite Diviner
Joined
Mar 24, 2015
Messages
426
Reaction score
416
I try to make sure that the graph of headers including each other is a directed acylic graph, but in the rare case that I absolutely need two headers to be able to depend on each other, I will just add a minimal amount of forward declarations to one of them such that it doesn't need to include the other and so the acylic property is maintained.

This is probably still my biggest problem, I still have tons of these and I'm not sure to resolve them.
Just to give an example:
"userinterface" is a member of "game". then various "uielements" are members of the "userinterface". then "buttons" are members of an "uielement". now when the button is pressed, how do I tell "game" what to do? because I'm not sure what else to do, I include the windows application's header in "uielements.cpp" and use "game" from there (I declared an instance of game in it). What's a better way to do this?
 
Upvote 0
Back
Top