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

15 Responses to “error LNK2001: unresolved external symbol private: static …”

  1. Steven Rick Says:

    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!

  2. admin Says:

    Glad to be useful. Thank you and good luck.

  3. Brett Says:

    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

  4. admin Says:

    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.

  5. db Says:

    Thanks a lot, this solved my error.

  6. Martin Says:

    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?

  7. admin Says:

    —————-
    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.

  8. Juan Says:

    Hey, thanks a lot! It was very very useful!

  9. Mira Says:

    Hey,
    Thanks for the “char * A::filename = NULL;” tip. The Linker error was driving me crazy. you saved me alot of time.

  10. Flite Says:

    Thanks!

  11. kotuk Says:

    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?

  12. Raghu Says:

    Thanks! now i’m able 2 finish in tiem :)

  13. Ahmad Says:

    Thank you !

  14. Vidhya Says:

    Thanks for the solution. It has relieved me of a 2 week linking error. Appreciate the effort!

  15. Ahmed Kaseb Says:

    Thanks so much … don’t have words to thank you

Leave a Reply