• Advertisement

Archived

This topic is now archived and is closed to further replies.

why isn't this working?

This topic is 6243 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

I am trying to set my new project like the article by Chris Hargrove that appeared here at gamedev a few months ago. He had all his global variables in one file called globals.h (or something like that), his main windows message loop and such in a file called main.cpp, another header file for a class called creature.h and a file for the class called creature.cpp. In globals.h he included creature.h, the global variables, definitions, etc. In creature.cpp he included globals.h. All the global variables in globals.h had the extern keyword infront of them. He also declared the same variables in main.cpp without the extern keyword in front of them. I am trying to get a similar setup running. My main message loop is called lightworks.cpp, my globals are kept in globals.h (with the extern keyword). My one header file (which is included in globals.h) is called functions.h, and last but not least there is the corresponding file functions.cpp, which includes globals.h. I cant seem to get things to work. Here is the first part of each file: lightworks.cpp: #include "globals.h" LPDIRECT3D8 g_pD3D = NULL; // Used to create the D3DDevice LPDIRECT3DDEVICE8 g_pd3dDevice = NULL; // Our rendering device globals.h: #include #include #include #include "functions.h" //----------------------------------------------------------------------------- // Global variables //----------------------------------------------------------------------------- extern LPDIRECT3D8 g_pD3D = NULL; // Used to create the D3DDevice extern LPDIRECT3DDEVICE8 g_pd3dDevice = NULL; // Our rendering device functions.h: nothing functions.cpp: #include "globals.h" Can anyone figure out what I am doing wrong? I have no clue (since this is only the second time I''ve tried this). "Never pet a burning dog." - Warcraft II

Share this post


Link to post
Share on other sites
Advertisement
It is telling me that I already declared the two global variables. I am also not sure if I need to include anything in functions.cpp. Any hints on this?

"Never pet a burning dog." - Warcraft II

Share this post


Link to post
Share on other sites
Aha - this might be it.
At the top of each header file, put this:
#ifndef NAME_OF_HEADER
#define NAME_OF_HEADER

then at the bottom:

#endif

This will stop multiple inclusion, and hopefully will fix your problem.

Share this post


Link to post
Share on other sites
quote:
Original post by Quantum

Aha - this might be it.
At the top of each header file, put this:
#ifndef NAME_OF_HEADER
#define NAME_OF_HEADER

then at the bottom:

#endif

This will stop multiple inclusion, and hopefully will fix your problem.




Couldn''t he replace this with
  
#pragma once


at the top of each header?

Share this post


Link to post
Share on other sites
quote:
Original post by NuffSaid

Couldn''t he replace this with

    
#pragma once


at the top of each header?


Yeah, but I like the other way better

Share this post


Link to post
Share on other sites
Thanks guys, I will give that a try as soon as I get home.

Share this post


Link to post
Share on other sites
Darn it. The #pragma once didn''t work. I was supposed to put in in every header file was I? Am I supposed to include any files in my functions.cpp?

I think I might have to e-mail Ben Dilts about this one again, but I would prefer if I didn''t have to pester him again.

"Never pet a burning dog." - Warcraft II

Share this post


Link to post
Share on other sites
Ok, try this:

  
//lightworks.cpp:


#include "globals.h"
LPDIRECT3D8 g_pD3D = NULL; // Used to create the D3DDevice

LPDIRECT3DDEVICE8 g_pd3dDevice = NULL; // Our rendering device


//functions.cpp:


#include "globals.h"


//globals.h:


#ifndef _GLOBALS_H_
#define _GLOBALS_H_

//include the headers that got cut off by the board..

#include "functions.h"

//-----------------------------------------------------------------------------

// Global variables

//-----------------------------------------------------------------------------

extern LPDIRECT3D8 g_pD3D = NULL; // Used to create the D3DDevice

extern LPDIRECT3DDEVICE8 g_pd3dDevice = NULL; // Our rendering device


#endif

//functions.h:

#ifndef _FUNCTIONS_H_
#define _FUNCTIONS_H_

#endif



Share this post


Link to post
Share on other sites
Nope. Didn''t work. Would you mind if I e-mailed you about this one Quantum?

I am probably doing something stupid but I don''t know it...

"Never pet a burning dog." - Warcraft II

Share this post


Link to post
Share on other sites
I think you're a little confused about some of these keywords. Let me explain, and if I come across a little patronising, hopefully you'll forgive me when the program starts working

Firstly, you can't just use 'extern' to allow you to put a variable in a header file. The way you need to do it, is to put some normal variable definitions in a CPP file. Doesn't matter which, but it makes sense to put them in something like globals.cpp. Then, you put references to those global variables in the header file globals.h. This is what makes them global. The globals themselves are not in the header file: only the extern references.

Example:
      
// globals.h

extern LPDIRECT3D8 g_pD3D; // note: NO '=NULL' or anything!


// globals.cpp, or anywhere else really

LPDIRECT3D8 g_pD3D = NULL; // this is where you initialise it.



That should fix any such problems you have regarding duplicated variables when it comes to link-time.

Secondly, the #pragma once directive can't stop a header being used more than once in a program. It just prevents it being used more than once for each compilation unit... ie. once for each .C or .CPP file you compile. Remember that each CPP file compiles independently to begin with, using whichever header files they need. It is only at the linker stage that they are combined into one... and therefore, any variables that are declared in header files may end up in more than one compiled CPP file, and therefore when the linker comes to put it all together, it finds duplicates. That is why you only put the actual variable definitions in one CPP file, and just put the extern declarations in the headers, which is enough to keep the compiler happy.

The #pragma once directive is roughly equivalent to using the standard inclusion guards on a header file, but is less portable. Therefore I recommend you get into the habit of putting your own inclusion guards in your own headers, like so:


#ifndef GLOBALS_H
#define GLOBALS_H
// ... the content of your header file here...
#endif // GLOBALS_H




Edited by - Kylotan on January 19, 2001 7:50:09 PM

Share this post


Link to post
Share on other sites
When you said portability, you mean between compilers (say Borland and Microsoft), right?

I will give it a try.

Never cross the thin line between bravery and stupidity.

Share this post


Link to post
Share on other sites
Oops! I didn''t mean to put the = NULL in the header file. my bad

quote:

Would you mind if I e-mailed you about this one Quantum?


It should probably work now, but if it still doesn''t, feel free

Share this post


Link to post
Share on other sites

  • Advertisement