importing header files and multiple definition
Hello,
I'm making a program in DevC++ 4.
I split my program into different cpp files, but I need the same variable in different cpp files.
One file can't use a variable that was declared in another cpp file, however if I declare it in both it gives multiple definition error, even if I use a header file and import it in both cpp files.
I read about a trick to add
#ifndef _FILENAME_H_
#define _FILENAME_H_
blahblah here
#endif
to the header file, but it STILL gives multiple definition errors...
Doesn't devcpp4 support #ifndef or something like that?
Are there other ways to use the same variable in multiple cpp files? I also tried all sorts of public/extern/static/global combinations but then the variable still only works in one cpp file
Thanks.
[edited by - Lode on May 21, 2002 6:39:53 PM]
quote: Original post by Lode
I split my program into different cpp files, but I need the same variable in different cpp files.
One file can''t use a variable that was declared in another cpp file, however if I declare it in both it gives multiple definition error, even if I use a header file and import it in both cpp files.
I read about a trick to add
#ifndef _FILENAME_H_
#define _FILE_H_
blahblah here
#endif
to the header file, but it STILL gives multiple definition errors...
Four things:
A) Your #ifndef and #define are incorrect. You cant'' put _FILENAME_H_ in one an _FILE_H_ in another.
B) You keep getting the same error because, as you said, you have defined it several times, hence the error.
C) What you need to do is use globals, search the forum as this topic is discussed each week
D) For knowing about common errors regarding organizing the code in files, take a look at:
http://www.gamedev.net/reference/programming/features/orgfiles/default.asp
Good luck.
The #define and #ifndef make sure you don''t declare the same things in the same header file more than once. If you declare the same thing in different header files, it still messes up. Probably the best way to use the same variable in two different files would be to pass the variable around to those files through parameters, since globals are bad. Of course, if you do want to actually use the same variable, look into extern variables.
- f l u c k y p o o
- the geek inside
- f l u c k y p o o
- the geek inside
- f l u c k y p o o
quote: Original post by Neodraco
A) Your #ifndef and #define are incorrect. You cant'' put _FILENAME_H_ in one an _FILE_H_ in another.
Heh, I just edited my post to change this, I typed it wrong in the post, not in c++
Thanks for the help, I''ll look into it.
In the header you should define the variable with the "extern" keyword in front of it.
And in *one* of the cpp files you should declare it without the extern keyword.
Then all files can use it just fine.
Example:
myheader.h:
extern int x;
-----------
myfile.cpp
#include "myheader.h"
int x;
< use x here >
------------
myfile2.cpp
#include "myheader.h"
< use x here >
[edited by - felonius on May 21, 2002 7:03:35 PM]
And in *one* of the cpp files you should declare it without the extern keyword.
Then all files can use it just fine.
Example:
myheader.h:
extern int x;
-----------
myfile.cpp
#include "myheader.h"
int x;
< use x here >
------------
myfile2.cpp
#include "myheader.h"
< use x here >
[edited by - felonius on May 21, 2002 7:03:35 PM]
Jacob Marner, M.Sc.Console Programmer, Deadline Games
GO THERE.
Documents [ GDNet | MSDN | STL | OpenGL | Formats | RTFM | Asking Smart Questions ]
C++ Stuff [ MinGW | Loki | SDL | Boost. | STLport | FLTK | ACCU Recommended Books ]
Documents [ GDNet | MSDN | STL | OpenGL | Formats | RTFM | Asking Smart Questions ]
C++ Stuff [ MinGW | Loki | SDL | Boost. | STLport | FLTK | ACCU Recommended Books ]
"Debugging is twice as hard as writing the code in the first place. Therefore, if you write the code as cleverly as possible, you are, by definition, not smart enough to debug it." — Brian W. Kernighan
This topic is closed to new replies.
Advertisement
Popular Topics
Advertisement