Including h files in subwindows

  • Thread starter HallsofIvy
  • Start date
  • Tags
    files
In summary, embedding the body of "this.h" between precompiler #if/#endif statements makes "#include "this.h" do nothing if the "this.h" has already been included.
  • #1
HallsofIvy
Science Advisor
Homework Helper
42,989
975
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?
 
Technology news on Phys.org
  • #2
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:
#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.
 
  • #3
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:
  • #4
D H said:
Code:
#ifndef _THIS_H_
#define _THIS_H_

// Body of this.h

#endif // #ifndef _THIS_H *** Do not place any code after this line
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.
 

1. Why do I need to include h files in subwindows?

Including h files in subwindows allows you to access the functions and variables defined in those files, making it easier to write and maintain your code. It also helps with organization and prevents code duplication.

2. How do I include h files in subwindows?

To include a h file in a subwindow, use the #include directive followed by the name of the h file. This will copy the contents of the h file into your subwindow's code, allowing you to use its functions and variables.

3. Can I include multiple h files in a subwindow?

Yes, you can include multiple h files in a subwindow by using the #include directive multiple times. Just make sure to avoid including the same h file more than once to avoid potential errors.

4. What is the difference between including h files in subwindows and using global variables?

Including h files in subwindows allows you to access functions and variables defined in those files, while using global variables allows you to access variables from any part of your code. Including h files is considered a better practice as it promotes encapsulation and avoids potential conflicts with variable names.

5. Are there any best practices for including h files in subwindows?

Yes, it is recommended to only include the necessary h files in each subwindow, rather than including all h files in every subwindow. This helps to keep your code organized and avoids potential issues with duplicate definitions. It is also good practice to use header guards in your h files to prevent multiple inclusions.

Similar threads

  • Programming and Computer Science
Replies
2
Views
378
  • Programming and Computer Science
Replies
6
Views
921
  • Programming and Computer Science
Replies
8
Views
1K
  • Programming and Computer Science
Replies
2
Views
1K
  • Programming and Computer Science
Replies
33
Views
2K
  • Programming and Computer Science
3
Replies
81
Views
5K
  • Programming and Computer Science
Replies
6
Views
8K
  • Programming and Computer Science
Replies
8
Views
3K
  • Programming and Computer Science
Replies
4
Views
3K
  • Programming and Computer Science
Replies
9
Views
862
Back
Top