Sign in to follow this  
Nick C.

'Unresolved external symbol' when using static struct in static method

Recommended Posts

Nick C.    225
Hello everyone

I'm writing an input managing class, and I've stumbled upon a problem right now. I've been trying to fix it for a long time, but I just don't see the problem. It's probably a minor detail (like always.. -.-), but I would really apreciate it if someone could take a look at it.

The error I'm getting is:

Error 13 error LNK2001: unresolved external symbol "private: static struct Input::Keyinfo * Input::KeyinfoArr" (?KeyinfoArr@Input@@0PAUKeyinfo@1@A) C:\Nick\D3D projects\DVD\Code\Chapter 25 Character Animation\SkinnedMesh\InputManager.obj

[source lang="cpp"]header file:

struct Input
{
//...some code
public:
enum Action{
MOVEFORWARD,
MOVEBACKWARD,
STRAFELEFT,
STRAFERIGHT,
JUMP};

static bool AllowAction(Action action);

private: //...some code

static const int ACTIONCOUNT = 5; // amount of possible controls/actions

struct Keyinfo
{
Action action;
int code; // virtual-key code associated with action
bool pressed;
bool down;
};

static Keyinfo KeyinfoArr[ACTIONCOUNT];
};

cpp file:

//...some code
bool Input::AllowAction(Action action)
{
for (int i = 0; i < ACTIONCOUNT; ++i)
{
if (KeyinfoArr[i].action == action)
{
if ((KeyinfoArr[i].action == Action::MOVEFORWARD ||
KeyinfoArr[i].action == Action::MOVEBACKWARD ||
KeyinfoArr[i].action == Action::STRAFELEFT ||
KeyinfoArr[i].action == Action::STRAFELEFT)
&& KeyinfoArr[i].down)
return true;
if (KeyinfoArr[i].action == Action::MOVEFORWARD && KeyinfoArr[i].pressed)
return true;
}
}
return false;
}[/source]

Share this post


Link to post
Share on other sites
SiCrane    11839
When you declare a static variable in a class, you still need to define it in a source file. In one source file you need to put:
[code]
Keyinfo Input::KeyinfoArr[ACTIONCOUNT];
[/code]

Share this post


Link to post
Share on other sites
the_edd    2109
And you'll want to initialise those Keyinfos at the point of that definition too, else you're in for some trouble. Edited by e?dd

Share this post


Link to post
Share on other sites
Nick C.    225
[quote name='SiCrane' timestamp='1352649116' post='4999934']
When you declare a static variable in a class, you still need to define it in a source file. In one source file you need to put:
[code]
Keyinfo Input::KeyinfoArr[ACTIONCOUNT];
[/code]
[/quote]

Stupid of me. Works like a charm now, thanks :)

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

Sign in to follow this