Dismiss Notice
Join Physics Forums Today!
The friendliest, high quality science and math community on the planet! Everyone who loves science is here!

Including h files in subwindows

  1. Apr 27, 2007 #1


    User Avatar
    Staff Emeritus
    Science Advisor

    I am having a problem with "include" files. If I put '#include "this.h"' in a subwindow, linking errors saying that the class defined in the h file is being defined twice. Since the h file for the subwindow is included in the code for the main window, I suppose that is why it is getting it twice. But what's going on here and how can I fix it?
  2. jcsd
  3. Apr 27, 2007 #2

    D H

    Staff: Mentor

    This is a typical problem confronted in C/C++, so typical that there is a very-widely used convention to overcome it.

    What you do is to embed the body of "this.h" between precompiler #if/#endif statements. This makes #include "this.h" do nothing if the "this.h" has already by #included:

    Code (Text):
    #ifndef _THIS_H_
    #define _THIS_H_

    // Body of this.h

    #endif // #ifndef _THIS_H *** Do not place any code after this line
    A big caveat: You have to make sure that the label _THIS_H_ is unique. If you use a name that is defined in some other .h file your #include "this.h" will never do anything.
  4. Apr 29, 2007 #3


    User Avatar
    Science Advisor
    Homework Helper

    DH's comment may well be the answer.

    Assuming your post was written accurately, another clue to your problem may be the difference between "declared" and "defined". It's an error to define something twice, but you can declare it as many times as you like (provided the declarations are consistent with each other).

    Examples of declarations:

    int myinteger;
    void myfunction();

    and definitions:

    int myinteger=123;
    void myfunction()
    // some code
    Last edited: Apr 29, 2007
  5. Apr 29, 2007 #4


    User Avatar
    Staff Emeritus
    Science Advisor
    Gold Member

    And a minor technical point: you shouldn't use a symbol beginning with an underscore or containing a double underscore for this purpose (such symbols are "reserved"). It is extremely rare that you will encounter a problem, though.
Know someone interested in this topic? Share this thread via Reddit, Google+, Twitter, or Facebook

Have something to add?