title: C++ static member variable of type enum won't compile
body: <p>I'm trying to write a class with a public enum, and a private static member variable of that enum type. I can initialise the value of the static variable, but if I try to access it in a class member function, my code won't link.  Here's a simple working example that will compile with:</p>
  
  <pre><code>g++ -o TestClass.o TestClass.cpp 
  </code></pre>
  
  <p>but then fails when I try to compile/link the main source file with:</p>
  
  <pre><code>g++ -o test TestClass.o testmain.cpp 
  </code></pre>
  
  <p>The error I get is:</p>
  
  <pre><code>Undefined symbols for architecture x86_64:
    "TestClass::_enum", referenced from:
        TestClass::printEnum()     in TestClass.o
  ld: symbol(s) not found for architecture x86_64
  </code></pre>
  
  <p>I'm using a Mac running OSX 10.7.5, with gcc 4.2.1.</p>
  
  <p>TestClass.h:</p>
  
  <pre><code>#ifndef TEST_CLASS_H
  #define TEST_CLASS_H
  
  class TestClass
  {
  public:
    TestClass() {};  
    void printEnum();
  
    typedef enum {A, B, C} MyEnum;
  
  private:
    static MyEnum _enum;
  
  
  };
  
  #endif
  </code></pre>
  
  <p>TestClass.cpp:</p>
  
  <pre><code>#include "TestClass.h"
  
  #include &lt;iostream&gt;
  using namespace std;
  
  TestClass::MyEnum _enum = TestClass::A;
  
  void TestClass::printEnum()
  {
    cout &lt;&lt; "Value of enum: " &lt;&lt; _enum &lt;&lt; endl;
  }
  </code></pre>
  
  <p>testmain.cpp:</p>
  
  <pre><code>#include "TestClass.h"
  
  int main()
  {
    TestClass tc;
  
    tc.printEnum();
  }
  </code></pre>