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

Started by
2 comments, last by Nick C. 11 years, 5 months ago
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.action == action)
{
if ((KeyinfoArr.action == Action::MOVEFORWARD ||
KeyinfoArr.action == Action::MOVEBACKWARD ||
KeyinfoArr.action == Action::STRAFELEFT ||
KeyinfoArr.action == Action::STRAFELEFT)
&& KeyinfoArr.down)
return true;
if (KeyinfoArr.action == Action::MOVEFORWARD && KeyinfoArr.pressed)
return true;
}
}
return false;
}[/source]
Advertisement
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:

Keyinfo Input::KeyinfoArr[ACTIONCOUNT];
And you'll want to initialise those Keyinfos at the point of that definition too, else you're in for some trouble.

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:

Keyinfo Input::KeyinfoArr[ACTIONCOUNT];



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

This topic is closed to new replies.

Advertisement