Click to See Complete Forum and Search --> : Variable sharing between include files


pfhott
March 27th, 2008, 04:29 PM
OK so I have a slight problem. I have a project in VS that I am trying to do some DirectX work with and having some trouble sharing a specific variable. I have one set of files for input "InputHandler.cpp/h" and another for controlling the rendering "WorldRender.cpp/h".

The main file simply initiates and manages the window. When it reads input it processes input through a function in the input handler. When it wants to render it processes through a function in the render file. What I have is a simple model rotation controlled by the arrow keys. So, when I want to rotate the model I push left arrow and the input handler sets a value to tell the renderer to rotate left.

Currently the variable(RotateY) is declared in "InputHandler.h" and both InputHandler and WorldRender are included by Main.cpp. How can I make it so that WorldRender sees the variable storing the value of rotation type in InputHandler? I tried including the input handler in WorldRender, but that results in multiple declarations of the variable RotateY since it is also included and initialized by Main.cpp. I konw the answer has to be fairly simple, it just isn't coming to me and I am starting to get frustrated...

S_M_A
March 27th, 2008, 04:58 PM
Declare it as extern in header and then declare variable in InputHandler (without extern)

IllegalCharacter
March 28th, 2008, 11:03 AM
There's a couple ways of doing this. First, are you using #ifndef/#defines at the start of your .h file? Like this:

#ifndef THIS_FILE_H__
#define THIS_FILE_H__

//stuff in the file

#endif


If you're doing this, then you can try doing externs.

In either InputHandler.cpp or WorldRender.cpp (but not both) declare the variable:

int myVar; //or whatever the declaration is

Then in the other one and in Main.cpp, declare the variable as well, but put extern before it:

extern int myVar;

Hope this helps.

laserlight
March 28th, 2008, 12:26 PM
I think that the use of extern is indeed the solution here, but note that IllegalCharacter's example of a header inclusion guard name is not advisable since names that contain a double underscore or begin with an underscore followed by an uppercase letter are reserved to the implementation for any use. Just remove one of the trailing underscores.

pfhott
March 28th, 2008, 02:12 PM
OK so first off, thank you for all of the quick and useful replies. Using the macro definitions in the header file worked... I think.

Now I get errors saying:
"error LNK2005: "struct IDirect3D9 * d3d" (?d3d@@3PAUIDirect3D9@@A) already defined in Main.obj"

It gives this error for a number of functions and parameters defined in WorldRender.h. With the macro definitions and also using "#pragma once" there should almost be no way that the functions are included and defined twice. I also don't think that is really the problem... Before I split the program into a number of different files, everything was in Main.cpp and I just cut and pasted the parts from the main cpp to WorldRender and InputHandler manually. Is it possible that the obj file still has some record of those functions/parameters and there is something I need to do to clear it out? I tried "Clean Solution" and "Refresh Project Toolbox Items" and even tried deleting the OBJ files manually, but I still get this error...

Why?

Hermit
March 28th, 2008, 02:28 PM
Have you declared the variable as extern in the header?

E.g. in a header file:
#ifndef INC_MY_HEADER
#define INC_MY_HEADER

extern int foo;

#endif

And in exactly one implementation (.cpp) file:
int foo = 0;

All other files simply include the header for the declaration of the global variable. Also note that global variables are generally regarded as Evil™.

pfhott
March 28th, 2008, 03:04 PM
OK, so I had to do the extern declaration as well... So what then is the point of having header files if you have to declare all of your variables twice (one with extern in the header and one without in the CPP) when you could simply declare them once in a cpp(other than documentation/reference)?

I suppose in the simple case I am trying to use them there isn't much of a difference. But do you have to do the same with class members and methods? I remember when I was in high school we simply declared it in the header and implemented it in the cpp so that the code would be cleaner... I don't remember having to use extern and declare all my variables twice. Maybe I'm just wrong

Martin O
March 28th, 2008, 03:22 PM
Declare it once in one header, with extern.

Define it once in one .cpp file, without extern.

Any .cpp files that need to access it should include the header.

Martin O
March 28th, 2008, 03:29 PM
So what then is the point of having header files if you have to declare all of your variables twice (one with extern in the header and one without in the CPP) when you could simply declare them once in a cpp(other than documentation/reference)?


You can't simply declare them once in a cpp file because then other .cpp files won't know about it. Thats just how c++ works.

Mercury048
March 28th, 2008, 03:52 PM
I'm pretty much a newbie as well, but isn't it standard practice in a case like this to declare the variable in "InputHandler.h", as the OP is doing, but then simply #include "InputHandler.h" in WorldRender.cpp?

Please tell me if I'm way off base.

pfhott
March 28th, 2008, 05:15 PM
OK, I understand it now... thanks for the help!