C++ static member variable of type enum won't compile -
i'm trying write class public enum, , private static member variable of enum type. can initialise value of static variable, if try access in class member function, code won't link. here's simple working example compile with:
g++ -o testclass.o testclass.cpp
but fails when try compile/link main source file with:
g++ -o test testclass.o testmain.cpp
the error is:
undefined symbols architecture x86_64: "testclass::_enum", referenced from: testclass::printenum() in testclass.o ld: symbol(s) not found architecture x86_64
i'm using mac running osx 10.7.5, gcc 4.2.1.
testclass.h:
#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
testclass.cpp:
#include "testclass.h" #include <iostream> using namespace std; testclass::myenum _enum = testclass::a; void testclass::printenum() { cout << "value of enum: " << _enum << endl; }
testmain.cpp:
#include "testclass.h" int main() { testclass tc; tc.printenum(); }
the code below declares file local variable _enum
of type testclass::myenum
. not providing definition static member variable.
testclass::myenum _enum = testclass::a;
to that, have define in appropriate class scope, this:
testclass::myenum testclass::_enum = testclass::a;
Comments
Post a Comment