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

Results 1 to 4 of 4
  1. #1
    The journey never ends. SYJourney is offline
    MemberRank
    Mar 2015 Join Date
    FrankfurtLocation
    427Posts

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

    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.


  2. #2
    Meh Rakeda is offline
    MemberRank
    Aug 2011 Join Date
    Nightmare RealmLocation
    374Posts

    Re: [C++] Easy fix to avoid these header problems

    - - - Updated - - -

    Quote Originally Posted by SYJourney View Post
    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.

  3. #3
    PeterRabbit retep998 is offline
    MemberRank
    Apr 2008 Join Date
    VanaLocation
    707Posts

    Re: [C++] Easy fix to avoid these header problems

    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++.

  4. #4
    The journey never ends. SYJourney is offline
    MemberRank
    Mar 2015 Join Date
    FrankfurtLocation
    427Posts

    Re: [C++] Easy fix to avoid these header problems

    Quote Originally Posted by retep998 View Post
    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?



Advertisement