Including h files in subwindows

  • Thread starter Thread starter HallsofIvy
  • Start date Start date
  • Tags Tags
    files
AI Thread Summary
The discussion addresses a common issue in C/C++ programming related to the use of header files and linking errors caused by multiple definitions. When a header file is included in multiple places, such as a subwindow and a main window, it can lead to errors indicating that a class is defined more than once. To resolve this, a widely accepted convention is to use preprocessor directives to prevent multiple inclusions. This involves wrapping the contents of the header file in `#ifndef`, `#define`, and `#endif` statements, ensuring that the header file is only included once. It's crucial to use a unique identifier for the header guard to avoid conflicts with other files. Additionally, the distinction between declaration and definition is highlighted; while declarations can occur multiple times, definitions must be unique to prevent errors. Caution is advised against using identifiers that start with an underscore or contain double underscores, as these are reserved.
HallsofIvy
Science Advisor
Homework Helper
Messages
42,895
Reaction score
984
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.
 
Dear Peeps I have posted a few questions about programing on this sectio of the PF forum. I want to ask you veterans how you folks learn program in assembly and about computer architecture for the x86 family. In addition to finish learning C, I am also reading the book From bits to Gates to C and Beyond. In the book, it uses the mini LC3 assembly language. I also have books on assembly programming and computer architecture. The few famous ones i have are Computer Organization and...
I have a quick questions. I am going through a book on C programming on my own. Afterwards, I plan to go through something call data structures and algorithms on my own also in C. I also need to learn C++, Matlab and for personal interest Haskell. For the two topic of data structures and algorithms, I understand there are standard ones across all programming languages. After learning it through C, what would be the biggest issue when trying to implement the same data...
Back
Top