Irritations with an extern variable

Hey there.

I've not been programming for long but I have a fair understanding of C++. At the moment I'm building an object orientated program which I've come to the conclusion requires an extern variable from another file. I've never used the extern keyword before but I thought I was aware of it's usage. Apparently I was wrong. Here is my dilemma.

So I have a class called Spaceship which of course has a header file and cpp file.
This class sets up my spaceship object.
I also have another class called D3DSetup which again has a header file and cpp file. This class sets up my direct3d device and such and deals with rendering.

Omitting a lot of unnecessary information this is what my D3D class looks like:
1
2
3
4
5
6
7
8
9
10
11
12
13
/*
inclusions of header files here: D3D9.h, Spaceship.h etc...
*/

class D3DSetup
{
public:
//functions go here...

private:
Spaceship* ship;
};


I then define this ship object in the D3DSetup.cpp file blah blah blah. The spaceship object has a Vector3 for it's position which i require to be manipulated with key presses. To do this I need to manipulate the spaceship objects move function from the window proc in my WinClass.cpp file. In WinClass.h i've added the include files for both Spaceship.h and D3DSetup.h where the ship variable is declared and defined.
Again with the unnecessary parts removed this is what my WinClass.cpp file looks like:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
//WinClass.cpp

extern Spaceship* ship;

LRESULT CALLBACK WndProc(HWND hWnd, UINT msg, WPARAM wParam, LPARAM lParam)
{
switch(msg)
{
case WM_KEYDOWN:
switch(wParam)
{
case VK_UP:
ship->MoveUp();
}
break;
}
}

When inserting the ship-> code in the WndProc function intellisense is picking up on the spaceship object. But when I compile I'm getting a lnk2001 error for the spaceship extern variable. I looked around to see if I was doing something wrong but all results point to making sure the correct include files are added. I've checked and double checked but D3DSetup.h is added in the WinClass.h file. I really don't know what I'm missing or doing wrong here. Any help would be appreciated. I'm sorry if I seemed to have rambled on. I thought this would be better than posting a dump load of code and saying "Read this and help plix!"
You don't seem to have a global variable called ship anywhere. What line 3 tells your compiler is that in some file there is a global variable called ship of type Spaceship*.
You can still make this work with extern if you have a global variable of type D3DSetup though.
Another option is to make all members of D3DSetup static. Then you won't need to use extern.
the ship variable is declared in D3DSetup.h and Defined in the D3DSetup.cpp file where it is first initialized to NULL as it's a pointer and then used to initialize the rendering of the object which is why i've included "D3DSetup.h" in the WinClass.h file because that is where the object is finds it's main use. I don't really want to make the members static as all members of the class need to be initialized first before use, if I don't make them static I leave out the possibility of making these kinds of mistakes. I did think about making them static at first but because there never needs to be more than one spaceship object i decided to make the variable external.
Last edited on
Ah, I think I understand whats going on here. I've made the Spaceship variable private and am trying to access that private data directly from another cpp file. Thanks for pointing that out Hamsterman =]
That's not the main problem.
Consider this: what if your D3DSetup.cpp had a line D3DSetup setup1, setup2;? I know you are not going to, but the compiler doesn't. Which ship would then be exported to WinClass.cpp?
If WinClass.cpp had extern D3DSetup setup1; there would be no ambiguity.
If D3DSetup was all static, it would not be possible to have two of them and, again, there would be no problem (you'd need to change some code though).
Topic archived. No new replies allowed.