error lnk2001

Started by
7 comments, last by littletray26 11 years, 1 month ago

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? :)

The majority of Internet Explorer users don't understand the concept of a browsing application, or that there are options.
They just see the big blue 'e' and think "Internet". The thought process usually does not get much deeper than that.

Worms are the weirdest and nicest creatures, and will one day prove themselves to the world.

I love the word Clicky
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;

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!

The majority of Internet Explorer users don't understand the concept of a browsing application, or that there are options.
They just see the big blue 'e' and think "Internet". The thought process usually does not get much deeper than that.

Worms are the weirdest and nicest creatures, and will one day prove themselves to the world.

I love the word Clicky
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.

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.
void hurrrrrrrr() {__asm sub [ebp+4],5;}

There are ten kinds of people in this world: those who understand binary and those who don't.

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.

The majority of Internet Explorer users don't understand the concept of a browsing application, or that there are options.
They just see the big blue 'e' and think "Internet". The thought process usually does not get much deeper than that.

Worms are the weirdest and nicest creatures, and will one day prove themselves to the world.

I love the word Clicky

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.

Worked on titles: CMR:DiRT2, DiRT 3, DiRT: Showdown, GRID 2, theHunter, theHunter: Primal, Mad Max, Watch Dogs: Legion

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.

void hurrrrrrrr() {__asm sub [ebp+4],5;}

There are ten kinds of people in this world: those who understand binary and those who don't.

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 :)

The majority of Internet Explorer users don't understand the concept of a browsing application, or that there are options.
They just see the big blue 'e' and think "Internet". The thought process usually does not get much deeper than that.

Worms are the weirdest and nicest creatures, and will one day prove themselves to the world.

I love the word Clicky

This topic is closed to new replies.

Advertisement