error LNK2001: unresolved external symbol private: static …

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 thoughts on “error LNK2001: unresolved external symbol private: static …

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

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

  4. 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?

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

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

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

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

  9. Thanks a lot!!
    Was breaking my head on this error for hours.
    Didn’t know initialization is necessary in this case.

  10. Pingback: Please help with MFC cross-threaded window access

Leave a Reply

Your email address will not be published. Required fields are marked *

You may use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>