Advertisement

REAL Globals

Started by March 03, 2000 06:26 PM
6 comments, last by BeanDog 24 years, 6 months ago
I declared some globals in my WinMain cpp file, but I can''t access them in my, for instance, DDrawBasics cpp file. How do you declare TOTAL GLOBALS?? Thanks [Edited by - BeanDog on January 15, 2006 12:50:58 PM]
Put the global in a header file, then reference that header file in every CPP file.

-Chris

---<<>>---
Chris Rouillard
Software Engineer
crouilla@hotmail.com
---<<>>--- Chris Rouillard Software Engineercrouilla@hotmail.com
Advertisement
I think what he said could be confused, by declaring the variable in the header it would cause multiple declarations (big no no). Here is a quick example

In WinMain.cpp (or whatever it is)

... //A bunch of includes
#include "myheader.h"

int myglobal;

.. //code for the file

In myheader.h

extern int myglobal; //declare it with the extern keyword

In your other cpp files

#include "myheader.h" //it is already declared in WinMain.cpp but the extern tells this file about it

...// code for the file

Hope this helps

-Omalacon
In my header file "globals.h":



//GLOBALS:
extern HWND hwnd; // generic window handle
extern LPDIRECTDRAW7 DDraw;
extern LPDIRECTDRAWSURFACE7 Primary;
extern LPDIRECTDRAWSURFACE7 Back;
extern LPDIRECTDRAWSURFACE7 BeanPic;
extern LPDIRECTDRAWSURFACE7 MapPic;

extern LPDIRECTINPUT7 DI;
extern LPDIRECTINPUTDEVICE7 Mouse;
extern LPDIRECTINPUTDEVICE7 Keyboard;
extern DIMOUSESTATE mState;
extern HANDLE hMouseEvent;

extern HRESULT hret;
extern char Keybuffer[256];

extern Creature Players[2];
extern int NumPlayers;

extern HDC BackHDC; //open to commands between DrawMain''s-Makes at end, destroys at begin of it
extern HDC MapHDC; //See above, but for Map Surface

And I included "globals.h" in each other header file, which were included in each cpp file.

Example h file: MapFns.h

#include

bool Filled(int x, int y);
int NextUp(int x, int y);
#include "Globals.h"

Build output:
Compiling...
Creature.cpp
f:\bensc++\dxgamev1.0\globals.h(3) : error C2146: syntax error : missing '';'' before identifier ''hwnd''
f:\bensc++\dxgamev1.0\globals.h(3) : fatal error C1004: unexpected end of file found
DDrawBasics.cpp
f:\bensc++\dxgamev1.0\globals.h(10) : error C2146: syntax error : missing '';'' before identifier ''DI''
f:\bensc++\dxgamev1.0\globals.h(10) : fatal error C1004: unexpected end of file found
WinMain.cpp
f:\bensc++\dxgamev1.0\globals.h(10) : error C2146: syntax error : missing '';'' before identifier ''DI''
f:\bensc++\dxgamev1.0\globals.h(10) : fatal error C1004: unexpected end of file found
MapFns.cpp
f:\bensc++\dxgamev1.0\globals.h(4) : error C2146: syntax error : missing '';'' before identifier ''DDraw''
f:\bensc++\dxgamev1.0\globals.h(4) : fatal error C1004: unexpected end of file found
Error executing cl.exe.

DXGame.exe - 8 error(s), 0 warning(s)


Any clues?
You need to declare the variables within ONE (not 2) source code file. Just declaring extern doesn''t declare the actual variable.
I declared them in WinMain.cpp
Advertisement
Did you include windows.h and ddraw.h before you included globals.h?
Sorry about my brief (and misleading) answer earlier

If you don''t include the ddraw.h header file before doing the externs, the compiler doesn''t realize that DDRAWSURFACE and such are a type. So, it assumes they are variable names (hence the request for a semicolon -- it naturally assumes that the next word should be the end of the statement).

-Chris

---<<>>---
Chris Rouillard
Software Engineer
crouilla@hotmail.com
---<<>>--- Chris Rouillard Software Engineercrouilla@hotmail.com

This topic is closed to new replies.

Advertisement