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!"