Full error message: error LNK2001: unresolved external symbol “private: static <type> * <Class>::<memberVariable>” (?<memberVariable>@<C@@0PADA)
Error occured on Visual Studio 2003 when compiling a C++ project.
The error occurred because memberVariable was a static variable and it wasn’t initialized.
Example:
class A { static char * filename; static void F(void); } ;
Fix:
char * A::filename = NULL;
The fix in my case is the following code added to the implementation of the <Class>:
<type> <Class>::<memberVariable> = <initValue>;
20 replies on “error LNK2001: unresolved external symbol private: static …”
Superb!
I’ve been wracking my head trying to resolve this during the implementation of the singleton pattern. My getSingleton public static (class) method was complaining about the resolved comilation problem mentioned above, because it was trying to access a private static attribute (which it was allowed to do).
Did the same thing basically:
MembersList* MembersList::mList = NULL;
Thanks so much!
Glad to be useful. Thank you and good luck.
I hit a similar issue, however when initializing the static member I get:
or C2864: ‘ClassName::pwszMemberString’ : only static const integral data member
s can be initialized within a class
You might have initialized the static variable inside the class definition like this:
class A
{
public:
static char * filename;
char * A::filename = “aaa”;
A(void);
~A(void);
};
This gives me the same error as you mentioned.
If you have this situation try and move the initialization in the cpp file of the class (outside of the class definition).
Good luck.
Thanks a lot, this solved my error.
I’m getting this same error but wherever I initialise the pointer ‘world’ I cant fix it. Error code is the following: error LNK2001: unresolved external symbol “private: static class CWorld * CWorld::world” (?world@CWorld@@0PAV1@A) World.obj DemoFinal
world.h is below
#pragma once
#include “drawing.h”
#include
#include
typedef unsigned char BYTE;
typedef unsigned long DWORD;
class CWorld
{
public:
CWorld(void);
~CWorld(void);
static void makeInstance();
static void deteleInstance();
static CWorld& getInstance();
void FillArray();
private:
static CWorld *world;
std::string mapArray[102];
int playerTileNum;
int playerPosX;
int playerPosY;
bool goalReady;
#define WORLD CWorld::getInstance()
};
world.cpp is below
#include “World.h”
#include
#include
#include
CWorld::CWorld(void)
{
}
CWorld::~CWorld(void)
{
}
void CWorld::deteleInstance()
{
delete world;
}
CWorld &CWorld::getInstance()
{
return *world;
}
void CWorld::makeInstance()
{
world = new CWorld;
}
Not sure how to solve it, I’ve barely even started the program and already a dead end, little help?
—————-
File World.h:
—————-
====================
If you read the whole post before asking the question you would have the answer figured out by yourself.
Hey, thanks a lot! It was very very useful!
Hey,
Thanks for the “char * A::filename = NULL;” tip. The Linker error was driving me crazy. you saved me alot of time.
Thanks!
Hi,
This solved my error, but….
I don’t understand why does it work???
Can someone explain this solution from the compiler/linker point of view?
Thanks! now i’m able 2 finish in tiem 🙂
Thank you !
Thanks for the solution. It has relieved me of a 2 week linking error. Appreciate the effort!
Thanks so much … don’t have words to thank you
Thank You. It helped me.
Thanks a lot!!
Was breaking my head on this error for hours.
Didn’t know initialization is necessary in this case.
[…] a global static variable, in normal-people talking terms. This link helped me a little bit: error LNK2001: unresolved external symbol private: static … | The Error Message If anyone knows the answer to my question above (at the top of this post), please let me know! […]
THANK YOU !!!!
Thanks !!! It’s so hard to find this problem. thanks! thanks! thanks!