-
Conflicting Header Files
I have 3 files, main.cpp, header1.h and header2.h.
header1.h only contains a variable: int hello
header2.h also contains the variable: int hello
main.cpp includes both headers, when I compile it says multiple definition.
I know why it does that but I was wondering, is there any way to make the definitions in the header files local to the actual file but global to the functions in it?
For example:
header1.h:
Code:
int hello;
void func1()
{
...
}
void func2()
{
...
}
both the functions in header1 need to use the variable hello,
and in header 2:
Code:
int hello;
void func1()
{
...
}
void func2()
{
...
}
both the functions in header2 also need to use the variable hello.
I cannot combine both header files into 1 header because Im splitting the sections of my code into headers in order to update it faster.
Is there anyway to make the definition of hello in both the headers invisible to main.cpp? but only allowing main.cpp to see the definitions of the funcs and being able to use them.
-
-
Or use static to give your global variable file scope.
-
thanx for the replies guys, i know the question was pretty basic lol, i kinda started out with the advanced before looking into the basics.
I was wondering, isnt static used to remember the variables previous value?
-
I just tried using static on all my headers, and the compiler still gives the redefinition errors.
-
Oh, I thought you were putting it in the .cpp files.
Since you're putting them all in .h files, use extern.
header1.h
Code:
#ifndef _HEADER1_H_
#define _HEADER1_H_
extern int hello ;
int getHello()
{
return hello ;
}
#endif
header2.h
Code:
#ifndef _HEADER2_H_
#define _HEADER2_H_
int hello ;
void setHello( int h )
{
hello = h ;
}
#endif
main.cpp
Code:
#include <stdio.h>
#include <stdlib.h>
#include "header1.h"
#include "header2.h"
int main(int argc, char *argv[])
{
setHello ( 4 ) ;
printf( "hello = %d\n", getHello() ) ;
getchar() ;
return 0;
}
-
I understand what youre trying to explain but I want each header to have its OWN variables but i want all of them to use the same name.
I have 3 headers, each named after a seperate class: Button, Textbox, and Label.
To make it easier to understand and read i want each of these headers to use the same names for the variables, e.g. cText instead of buttonText, labelText, and textboxText.
So far the static thing didnt work, and im not really sure what he meant by namespaces, it sounds kinda advanced. Im jsut looking for a really simple way to do this and im pretty sure it exists.
If im doing something wrong with the statics can someone show me how to do it right:
header1
Code:
static int hello;
static char cText;
func1()
{
...
}
func2()
{
...
}
header2
Code:
static int hello;
static char cText;
func1()
{
...
}
func2()
{
...
}
func1 and func2 are made to suit their own headers so I cant have them conflicting with the other headers, they are named differently so its all good except for the variable definitions.
-
Ah, I see what you mean. In that case, I would recommend you create corresponding .cpp (or .c) files for your headers.
Header 1
Code:
#ifndef _HEADER1_H_
#define _HEADER1_H_
int getHello1() ;
void setHello1( int ) ;
#endif
Header 2
Code:
#ifndef _HEADER2_H_
#define _HEADER2_H_
void setHello2( int ) ;
int getHello2() ;
#endif
Implementation file for Header 1
Code:
#include "header1.h"
static int hello ;
int getHello1()
{
return hello ;
}
void setHello1( int h )
{
hello = h ;
}
Implementation file for Header 2
Code:
#include "header2.h"
static int hello ;
int getHello2()
{
return hello ;
}
void setHello2( int h )
{
hello = h ;
}
Main Source File
Code:
#include <stdio.h>
#include <stdlib.h>
#include "header1.h"
#include "header2.h"
int main(int argc, char *argv[])
{
setHello1 ( 1 ) ;
setHello2 ( 2 ) ;
printf( "1st hello = %d\n", getHello1() ) ;
printf( "2nd hello = %d\n", getHello2() ) ;
getchar() ;
return 0;
}
-
PERFECT thanx man, anyway i can rate you lol? you helped out alot, possibly just saved me from having to rename 100s of variables :S
thanx again!
-
ok this just got a lil bit more ugly lol i tried converting them but it got VERY confusing, i just messed up all the headers and stuff :S is there anyway to do this without the .c implementation.
-
I don't think you can do it just using header files. Header files (.h) are generally used for declarations, not definitions.
Declaring (and thus defining) a global variable in header1 and header2 with the same name will always generate a conflict if both headers are included in the same source file.
-
Rules of thumb:
- Never instantiate anything in a header file
- Never implement code in a header file unless it's within the declaration of a class/struct, a template method/function, or an inline function
So if you want multiple instances of a variable with the same name, then you need multiple source files, or multiple namespaces within the same source file.
gg
-
If something goes wrong, it's usually a better idea to figure out what's wrong and fix it rather than try and bypass the problem by doing something else (i.e. just going back to putting everything in the headers).
What exactly is "ugly"? Errors? Can you post some code? :cool:
-
Well i did what Dante recommended and now its not "ugly" any more lol, by that i meant LOTS of errors and the code was very hard to read.
I now have to include 5 source and 5 header files so my program is getting a little cloggy.
I was wondering if it is possible to make the code that dante showed into a LIB so that I only have to link the LIB in order to use all the source and header files.
-
>>I now have to include 5 source and 5 header files
You split 2 headers into 5? Why? :eek: