19
Jul
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>;

October 31st, 2007 at 5:56 am
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!
October 31st, 2007 at 9:24 am
Glad to be useful. Thank you and good luck.
April 16th, 2008 at 3:50 pm
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
April 16th, 2008 at 5:19 pm
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.
June 9th, 2008 at 9:36 am
Thanks a lot, this solved my error.
March 8th, 2009 at 9:46 am
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?
March 8th, 2009 at 11:10 am
—————-
File World.h:
class CWorld { public: CWorld(void); ~CWorld(void); static CWorld * getInstance(); private: static CWorld *world; };—————-
File World.cpp #include ".\world.h" CWorld::CWorld(void) { } CWorld::~CWorld(void) { } CWorld * CWorld::getInstance() { if(world == NULL) { world = new CWorld(); } return world; } CWorld* CWorld::world = NULL;====================
If you read the whole post before asking the question you would have the answer figured out by yourself.
April 20th, 2009 at 11:30 pm
Hey, thanks a lot! It was very very useful!
May 14th, 2009 at 6:10 am
Hey,
Thanks for the “char * A::filename = NULL;” tip. The Linker error was driving me crazy. you saved me alot of time.
June 3rd, 2009 at 11:36 am
Thanks!
June 10th, 2009 at 3:24 am
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?
December 5th, 2009 at 11:56 pm
Thanks! now i’m able 2 finish in tiem
December 26th, 2009 at 11:51 am
Thank you !
March 4th, 2010 at 6:09 pm
Thanks for the solution. It has relieved me of a 2 week linking error. Appreciate the effort!
March 31st, 2010 at 10:23 am
Thanks so much … don’t have words to thank you