PDA

View Full Version : Including h files in subwindows


HallsofIvy
Apr27-07, 11:11 AM
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?

D H
Apr27-07, 11:44 AM
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:

#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.

AlephZero
Apr29-07, 05:29 PM
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
}

Hurkyl
Apr29-07, 05:47 PM
#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.