# error lnk2001

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

## Recommended Posts

Hey GameDev :)

Just a quick one today, I can't seem to figure out why this code:


#include <d3d9.h>
#include <d3dx9.h>

#pragma comment (lib, "d3d9.lib")
#pragma comment (lib, "d3dx9.lib")

class Player
{
private:

public:
static void Initialize(LPDIRECT3DDEVICE9* d3dDevice);
static void Draw(LPD3DXSPRITE sprite, LPDIRECT3DTEXTURE9* texture);
static void Update();
};

void Player::Initialize(LPDIRECT3DDEVICE9* d3dDevice)
{
}

void Player::Draw(LPD3DXSPRITE sprite, LPDIRECT3DTEXTURE9* texture)
{
}

void Player::Update()
{

}


Feels like telling me this:

Error 1 error LNK2001: unresolved external symbol "private: static struct tagRECT Player::paddleTexture" (?paddleTexture@Player@@0UtagRECT@@A)

Error 2 error LNK2001: unresolved external symbol "private: static struct D3DXVECTOR3 Player::paddlePosition" (?paddlePosition@Player@@0UD3DXVECTOR3@@A)

Could I get one of you lovely people to help me out? :)

##### Share on other sites

So far you only declared static variables, but didn't define them, to fix that add this in your source file:

D3DXVECTOR3 Player::paddlePosition;
RECT Player::paddleTexture;

##### Share on other sites

Thank you Zaoshi, that fixed it :)

Would you, or anyone else, mind explaining for myself, and anyone else with the same problem, Why I need to do this, and also where I should put it? I just put that code into the global scope (outside the class or functions), is there a better place?

I find it's easier for myself to learn, and remember, if I understand why and how :)

Thanks again!

##### Share on other sites
There's not much more to add. In C++ you have to define variables before you can use them, and a static class member is only declared inside the class definition, otherwise you would end up with one in every source file that included it.

The real question is why you are using static variables. If all the statics were removed, the variables would live inside the player object and there would be no need to define them separately.

##### Share on other sites

EWClay, on 11 Mar 2013 - 08:39, said:
There's not much more to add. In C++ you have to define variables before you can use them, and a static class member is only declared inside the class definition, otherwise you would end up with one in every source file that included it.

The real question is why you are using static variables. If all the statics were removed, the variables would live inside the player object and there would be no need to define them separately.

This.

You need to look at what the 'static' keyword actually does when you use it within a class. It seems like you're trying to create a singleton and just going about it in entirely the wrong way. An easy solution to that is: do not use a singleton here. Typically you should avoid singletons whenever possible.

##### Share on other sites

ou need to look at what the 'static' keyword actually does when you use it within a class.

I have reasons for wanting them to be static, and I do know what the static keyword does. Thanks.

##### Share on other sites

ou need to look at what the 'static' keyword actually does when you use it within a class.

I have reasons for wanting them to be static, and I do know what the static keyword does. Thanks.

Then what is that reason because you have just made it so that you can never add a second local player without changing that class if you want him to control a separate paddle.

There is usually no good reason to use a static member variable unless it's a piece of data that needs to be the same for all instances to work.

##### Share on other sites

ou need to look at what the 'static' keyword actually does when you use it within a class.

I have reasons for wanting them to be static, and I do know what the static keyword does. Thanks.

Not trying to jump on you there. My post sounded a bit harsh and I didn't intend that. I was just trying to express that 'static' behaves differently within a class declaration than it does in other places.

##### Share on other sites

Not trying to jump on you there. My post sounded a bit harsh and I didn't intend that. I was just trying to express that 'static' behaves differently within a class declaration than it does in other places.

ou need to look at what the 'static' keyword actually does when you use it within a class.

I have reasons for wanting them to be static, and I do know what the static keyword does. Thanks.

Then what is that reason because you have just made it so that you can never add a second local player without changing that class if you want him to control a separate paddle.

There is usually no good reason to use a static member variable unless it's a piece of data that needs to be the same for all instances to work.

l'm sorry if the way I responded sounded a bit rude. You're both right though, although I do know what the static keyword does, I've no idea what I was thinking putting it there.

Thank you both for helping me resolve the problem, and more :)