2

I'm writing a little DirectX model viewer.

I just finished writing my scene manger.

I will only ever have 1 scene manger object so my idea was to create a global pointer of type "scnManger" and then set it when i create my scene manger object.

This would let me get to it from any where in my application.

I'm getting a compile error though:

1>shaderViewer.obj : error LNK2005: "class scnManger * sceneManger" (?sceneManger@@3PAVscnManger@@A) already defined in sceneManager.obj
1>C:\Users\Greg\Documents\Visual Studio 2010\Projects\ShaderViewer\Debug\ShaderViewer.exe : fatal error LNK1169: one or more multiply defined symbols found

right now I have 3 files

sceneManger.h:

// Our global scene manger variable

scnManger* sceneManger;

shadherViewer.cpp (winMain & includes sceneManger.h):

scnManger shaderViewerScnManger; 
sceneManger = &shaderViewerScnManger;

sceneManger.cpp (includes sceneManger.h):

I use the methods of my scene manger object in here for various things.

First I would like to understand why I am getting the error, and am also open to any suggestions about a better way to handle this. I wasn't sure if using a global variable like this was a good idea or not.

4

1 に答える 1

6

You should not define global variables in .h files. You should declare them in .h, like this:

extern scnManger* sceneManger;

and then define them in one cpp file, like this:

scnManger* sceneManger;

Otherwise, every cpp file that includes your .h file will declare the sceneManger variable, resulting in a name collision.

于 2012-06-17T05:32:18.073 に答える