Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations strongm on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Header problems

Status
Not open for further replies.

Philisoft

Programmer
Oct 3, 2000
2
US
Hey hey
I'm having some problems with my header files; I've been programming in C++ for awhile now but haven't really had much experience seperating my stuff into cpp and header files.

Anyway, I searched through this forum for a solution and none presented itself, so I'll go ahead and describe my problem.

I put some global declarations into a header file, because of a few of my other cpp files need them.. so I just include the file from the various CPP files. I also put a few function prototypes in the header. Right now I'm just experimenting with two source files including the header - when one source file includes it, everything works fine, and when the other source file includes it everything gets declared twice (I think). I get the errors that all of my globals have already been declared (from the other source, since it already included my header file) but not errors from the function prototypes..

I've written my header in everyway possible to keep things from being declared twice, using the various methods:
#ifndefined myheader_h
#define myheader_h
globals etc.
#endif

and like so:

#if !defined(MAINDEFS_H)
#define MAINDEFS_H
..globals etc.
#endif

But everytime I compile it and have two of my sources include it, everything gets declared twice..maybe there's an inheritence problem I'm not seeing here, like the #define not carrying over to the other source file when it calls the header (I'm not sure if that's possible or not) or something relative to that.. I've looked through many, many people's header files, borlands and microsoft's stuff, they all use methods like this and seem to work, but for some reason it will not work in my project!! GRR!! I'm compiling with MS Visual C++ 6.0 and the project type is a blank win32 app.. maybe I need to use a namespace call or something? I've seen those in various programs but never thought I'd need them..

Anyway anyone who has encountered this please help, I was thinking it was the compiler but tried it on another Visual C++ on another computer at school and it does the same thing. No syntax in my code anywhere or in my header file, so I have no idea why it's declaring the globals twice (maybe the function prototypes as well, but perhaps this doesn't generate an error for some reason).

Thanks to anyone who can help! [sig][/sig]
 
I can think of 2 possible approaches 1) Have a separate header (.h file) for each .cpp; or 2) #Include the header from only one .cpp file. Typically, in my experience, each .h file holds the declarations for variables, functions, classes, etc, while the .cpp file contains the definitions. To ease the strain on my brain, I tend to prefer approach 1) from above. Approach 2) would probably involve some juggling and experimenting by you to make sure the declarations are made before you define them...not impossible, but a headache to me.

CN [sig][/sig]
 
Hey, thanks for the advice. The problem is, if I don't include the same header by both files, then the global declared in the header will work for the cpp file that includes it, but not for the other cpp file.

For instance, I have two CPP files that both make use of the same global variable. I have functions in both files that need the variable to operate. If I include the header in only 1 file, then only one cpp will have the global defined..anyone else have this experience? It's like I can define a global in a header, but if the CPP file doesn't include that exact header (even tho its included somewhere else) then the global doesn't exist..

Damnit this is killing me. Help!! [sig][/sig]
 
What about declaring the global in a .cpp file? You may have to experiment to make sure the .cpp that has the global gets compiled first, so that when the compiler encounters it in the other .cpp, it will have already "seen" it.

Another option, considered safer by many C++'ers, is to make it a static member variable in one of your classes, rather than a global.

CN [sig][/sig]
 

Check the settings Alt+F7, C/C++, Category Precompiled Headers.

If you are using the precompiled header file include it in all your .cpp files (#include stdafx.h). Class wizard does this for you if you create a new class using class wizard but if you import your code it will not and the error returned indicates multiple definitions of variables for some strange reason.

Let me know if this helps,

Brother C
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top