I am a C programmer, but had learnt C++ @school longtime back. Now I am trying to write code in C++ but getting compiler error. Please check and tell me whats wrong with my code.
typedef class _filter_session
{
private:
static int session_count; /* Number of sessions count -- Static */
public:
_filter_session(); /* Constructor */
~_filter_session(); /* Destructor */
}FILTER_SESSION;
_filter_session::_filter_session(void)
{
(this->session_count)++;
return;
}
_filter_session::~_filter_session(void)
{
(this->session_count)--;
return;
}
The error that I am getting is
“error LNK2001: unresolved external symbol “private: static int _filter_session::session_count” (?session_count@_filter_session@@0HA)”
I am using Visual Studio 2005 by the way.
Plz plz help me.
Regards,
Microkernel
staticvariables need to be defined outside of the class body somewhere. The declaration inside the class body is just a declaration.E.g. at global scope:
You need to ensure that this definition occurs only once in the program so usually you would place it in a source file (
.ccor.cpp) and not a header file which is included in more than once translation unit.For portability you should avoid class names that start with an
_. There is also little need totypedefyour class name.class Name { //...introduces a type name in C++, you wouldn’t have to useclass Nameto refer to the type.