Including h files in subwindows

  • Thread starter Thread starter HallsofIvy
  • Start date Start date
  • Tags Tags
    files
Click For Summary

Discussion Overview

The discussion revolves around issues related to including header files in C/C++ programming, specifically when using header files in subwindows and the resulting linking errors due to multiple definitions of classes. Participants explore solutions to prevent these errors and clarify the distinction between declarations and definitions.

Discussion Character

  • Technical explanation
  • Conceptual clarification
  • Debate/contested

Main Points Raised

  • One participant describes encountering linking errors when including a header file in a subwindow, suspecting that the header is being included multiple times due to its inclusion in the main window.
  • Another participant suggests using preprocessor directives (#ifndef, #define, #endif) to prevent multiple inclusions of the header file, emphasizing the need for a unique label to avoid conflicts.
  • A third participant highlights the importance of distinguishing between declarations and definitions, noting that while definitions should not be repeated, declarations can occur multiple times if they are consistent.
  • A later reply adds a technical note regarding the use of symbols starting with an underscore or containing double underscores, indicating that these are reserved and should be avoided for unique labels.

Areas of Agreement / Disagreement

Participants generally agree on the use of preprocessor directives to manage header file inclusions, but there is no consensus on the best practices for naming conventions and the implications of declarations versus definitions.

Contextual Notes

There are limitations regarding the assumptions made about the participant's initial problem description and the potential for misunderstanding the distinction between declarations and definitions. The discussion does not resolve whether the proposed solutions will fully address the linking errors.

HallsofIvy
Science Advisor
Homework Helper
Messages
42,895
Reaction score
983
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
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.
 
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:
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.
 

Similar threads

  • · Replies 2 ·
Replies
2
Views
3K
Replies
6
Views
2K
  • · Replies 2 ·
Replies
2
Views
2K
  • · Replies 8 ·
Replies
8
Views
2K
  • · Replies 33 ·
2
Replies
33
Views
3K
Replies
81
Views
8K
  • · Replies 6 ·
Replies
6
Views
12K
  • · Replies 2 ·
Replies
2
Views
2K
  • · Replies 8 ·
Replies
8
Views
4K
  • · Replies 9 ·
Replies
9
Views
2K