C++ static variables
I'm working on a project in C++ and I created a static member function and a static data member in one of my classes. When I tried to compile I kept getting an unresolved external error that was pointing to my static data member and I couldn't figure out what the problem was. Finally after some research (via Google, Msdn, and Stroustrup) I figured out that you have to define the static data member outside the class declaration.
I don't consider myself to be a novice C++ programmer but I don't think I ever knew this rule. Am I the only one who didn't know this? I guess for the most part I don't use static member variables very much, I know I have used local function static variables and static functions but I guess I have never used a static data member because I know I would have remembered this rule. At any rate I know now and knowing is half the battle ;)
Here is a small code snippet to show what needs to be done:
1 // MyClass.h 2 class MyClass 3 { 4 public: 5 static int GetCount() { return count; } 6 private: 7 static int count; 8 }; 1 // MyClass.cpp 2 #include "MyClass.h" 3 int MyClass::count; // Also do any initialization if needed.