Globals [RESOLVED]
Yes, I know their evil, but I need them.
I have 2 files that need to access a SDL_Surface named screen.
They both include "Globals.h" and both have the using namespace std; expression. So, in the globals header, I have extern SDL_Surface *screen. In the main.cpp file, where my main stuff is, right after #include "Globals.h", I have SDL_Surface *screen = NULL. In my graphics functions file, I have SDL_Surface *screen. So I'm getting the linker errors:
--------------------Configuration: MGSDL - Win32 Debug--------------------
Compiling...
GameFunctions.cpp
Linking...
GameFunctions.obj : error LNK2005: "struct SDL_Surface * screen" (?screen@@3PAUSDL_Surface@@A) already defined in Main.obj
Debug/MGSDL.exe : fatal error LNK1169: one or more multiply defined symbols found
Error executing link.exe.
MGSDL.exe - 2 error(s), 0 warning(s)
What am I doing wrong? Globals are confusing...(if there's a better solution to having multiple files access the same variables, please post it!)
[Edited by - orcfan32 on May 8, 2006 1:02:49 PM]
are you using?
#ifndef GLOBALS_H
#define GLOBALS_H
/// header file stuff here....
#endif
to wrap your header file?
theTroll
#ifndef GLOBALS_H
#define GLOBALS_H
/// header file stuff here....
#endif
to wrap your header file?
theTroll
Globals aren't always a bad thing......it just depends on what you're using them for. In this case I think using them would be ok.
Main.cpp:
- SOURCE REMOVED -
Globals.h:
- SOURCE REMOVED -
And GameFunctions.cpp (Be warned that this is incomplete, while I was waiting for replies, I was working on the function in here):
- SOURCE REMOVED -
(And yes, I'm aware of the TTF errors; ignore them.)
[Edited by - orcfan32 on May 8, 2006 12:14:39 PM]
- SOURCE REMOVED -
Globals.h:
- SOURCE REMOVED -
And GameFunctions.cpp (Be warned that this is incomplete, while I was waiting for replies, I was working on the function in here):
- SOURCE REMOVED -
(And yes, I'm aware of the TTF errors; ignore them.)
[Edited by - orcfan32 on May 8, 2006 12:14:39 PM]
Look at your linking error:
GameFunctions.obj : error LNK2005: "struct SDL_Surface * screen" (?screen@@3PAUSDL_Surface@@A) already defined in Main.obj
This look at your main.cpp and your GameFunctions.cpp .. you've defined screen in BOTH, which is exactly what your linker error is saying. Take out the define in your GameFunctions.cpp
GameFunctions.obj : error LNK2005: "struct SDL_Surface * screen" (?screen@@3PAUSDL_Surface@@A) already defined in Main.obj
This look at your main.cpp and your GameFunctions.cpp .. you've defined screen in BOTH, which is exactly what your linker error is saying. Take out the define in your GameFunctions.cpp
Only declare the variable in the header and in any other cpp that needs it use extern
extern SDL_Surface *screen;
Cheers
Chris
extern SDL_Surface *screen;
Cheers
Chris
As an alternative idea, you could create something along the lines of:
This would allow you to encapsulate initializing and cleaning up after SDL.
class SDLApplication{ private: SDL_Surface* mScreen; ... public: bool Resize( int width, int height ); ...};
This would allow you to encapsulate initializing and cleaning up after SDL.
This topic is closed to new replies.
Advertisement
Popular Topics
Advertisement