1

Possible Duplicate:
What does it mean to have an undefined reference to a static member?

Currently I have the following code File: TestClass.h

class TestClass
{

private:
    int i;
    static TestClass* TClass;

public:

    static TestClass* GetClass()
    {
        if(TClass==NULL)
        {
            TClass = new TestClass();
            return TClass;
        }
        else
        {
            return TClass;
        }
    }//end method

    int Geti()
    {
        return i;
    }

    void Seti(int a)
    {
        i = a;
    }
};

Now I have a method in my cpp file after including the header as

declspec(dllexport) int __stdcall GetVar()
{

    TestClass *TClass = TestClass::GetClass();
    return TClass->Geti();
}

The error I get is:

Error   8   error LNK2001: unresolved external symbol "private: static class TestClass * TestClass::TClass" (?TClass@TestClass@@0PEAV1@EA)  
Community
  • 1
  • 1
Casper_2211
  • 1,075
  • 5
  • 14
  • 25
  • 1
    look at http://stackoverflow.com/questions/12573816/what-is-an-undefined-reference-unresolved-external-symbol-error-and-how-do-i-fix – Rapptz Jan 30 '13 at 01:58

1 Answers1

2

YOu declared but not defined yours static member

TestClass* TClass;

In a .cpp file initilize:

TestClass* TestClass::TClass=NULL;
qPCR4vir
  • 3,521
  • 1
  • 22
  • 32
  • +1: I'm not entirely sure why this was down-voted. Unless everyone else has psychic goggles and knows that somewhere that static class-var `TestClass::TClass` is allocated in static space outside the class, this is the correct answer, and is precisely what the compiler is complaining about. – WhozCraig Jan 30 '13 at 06:53