• Advertisement
Sign in to follow this  

error lnk2001

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

If you intended to correct an error in the post then please contact us.

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:
static D3DXVECTOR3 paddlePosition;
static RECT paddleTexture;
 
public: 
static void Initialize(LPDIRECT3DDEVICE9* d3dDevice);
static void Draw(LPD3DXSPRITE sprite, LPDIRECT3DTEXTURE9* texture);
static void Update();
};
 
void Player::Initialize(LPDIRECT3DDEVICE9* d3dDevice)
{
paddlePosition = D3DXVECTOR3(0, 0, 0);
paddleTexture.bottom = 28;
paddleTexture.left = 0;
paddleTexture.right = 136;
paddleTexture.top = 0;
}
 
void Player::Draw(LPD3DXSPRITE sprite, LPDIRECT3DTEXTURE9* texture)
{
sprite->Draw(*texture, &paddleTexture, &D3DXVECTOR3(0, 0, 0), &paddlePosition, D3DCOLOR_ARGB(255, 255, 255, 255));
}
 
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 this post


Link to post
Share on other sites
Advertisement

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 this post


Link to post
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 this post


Link to post
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 this post


Link to post
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 this post


Link to post
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 this post


Link to post
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 this post


Link to post
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 this post


Link to post
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 :)

Share this post


Link to post
Share on other sites
Sign in to follow this  

  • Advertisement