Thread: header file placment, where to?

Hybrid View

Previous Post Previous Post   Next Post Next Post
  1. #1
    Registered User
    Join Date
    Apr 2006
    Posts
    2,149
    It looks like you're using Display and Visual inside img.h, which is included in options.cpp. The correct thing to do here is to make img.h include the X11 headers it needs. You may also want to not include img.h in options.cpp, if possible.

    Another thing you might be able to do in many similar cases, is instead of #including the hearer that defines the classes that are used as pointers and references in your headers, you can put in a forward declaration of those classes. For example at the top of img.h you could put "class Display;" and "class Visual;". This is simpler and compiles quicker.

    Finally, in order to avoid this kind of problem, you can change the order of #include directives in your cpp files to put system libraries last. This way, if a header needs a system that it does not include, you will get an error immediately, instead of much later when you try to include that header in a new file.
    It is too clear and so it is hard to see.
    A dunce once searched for fire with a lighted lantern.
    Had he known what fire was,
    He could have cooked his rice much sooner.

  2. #2
    Banned
    Join Date
    Aug 2017
    Posts
    861
    Quote Originally Posted by King Mir View Post
    It looks like you're using Display and Visual inside img.h, which is included in options.cpp. The correct thing to do here is to make img.h include the X11 headers it needs. You may also want to not include img.h in options.cpp, if possible.

    Another thing you might be able to do in many similar cases, is instead of #including the hearer that defines the classes that are used as pointers and references in your headers, you can put in a forward declaration of those classes. For example at the top of img.h you could put "class Display;" and "class Visual;". This is simpler and compiles quicker.
    that is something I've never seen before, as of now I am having a display issue. this same program in C I didn't have this same issue, but that is for another post.

    Finally, in order to avoid this kind of problem, you can change the order of #include directives in your cpp files to put system libraries last. This way, if a header needs a system that it does not include, you will get an error immediately, instead of much later when you try to include that header in a new file.
    that was another thought in my head, what header files to list first, second, third, etc.. it has to matter I am sure. I usually use a loose rule of thumb, system files first in < > then local header files after that using " " (quotes) and the header for the cpp I'm in last.

  3. #3
    Registered User
    Join Date
    Apr 2006
    Posts
    2,149
    Quote Originally Posted by userxbw View Post
    that is something I've never seen before, as of now I am having a display issue. this same program in C I didn't have this same issue, but that is for another post.
    Display issues means your code compiles and are therefore a sign of a different problem.

    that was another thought in my head, what header files to list first, second, third, etc.. it has to matter I am sure. I usually use a loose rule of thumb, system files first in < > then local header files after that using " " (quotes) and the header for the cpp I'm in last.
    What I'm suggesting is reversing this order.
    It is too clear and so it is hard to see.
    A dunce once searched for fire with a lighted lantern.
    Had he known what fire was,
    He could have cooked his rice much sooner.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 11
    Last Post: 09-25-2011, 12:22 AM
  2. Replies: 30
    Last Post: 06-19-2006, 12:35 AM
  3. Including header file with in the header file
    By Arafat_211184 in forum C Programming
    Replies: 13
    Last Post: 12-19-2005, 10:03 AM
  4. Replies: 4
    Last Post: 12-14-2005, 02:21 PM
  5. Replies: 6
    Last Post: 04-02-2002, 05:46 AM

Tags for this Thread