开发者

C++ Class Static variable problem - C programmer new to C++

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();         /* Co开发者_JS百科nstructor */
    ~_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


static variables 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:

int _filter_session::session_count;

You need to ensure that this definition occurs only once in the program so usually you would place it in a source file (.cc or .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 to typedef your class name. class Name { //... introduces a type name in C++, you wouldn't have to use class Name to refer to the type.


Not to do with your problem, but in C++ there is no need to typedef classes and structs like this:

typedef class _filter_session
{
  ...
}FILTER_SESSION;

You can and should simply say:

class filter_session
{
  ...
};

Also, c;lass names should not normally be in uppercase, as people will confuse them with macros. and you rarely need to use the this-> construct - your code certainly does not.


You need to initialize the static variable. This code actually compiles:

typedef class _filter_session
{
private:
    static int session_count;  /* Number of sessions count -- Static */    
public:
    _filter_session();         /* Constructor */
    ~_filter_session();        /* Destructor */
}; // FILTER_SESSION;

int _filter_session::session_count = 0;


_filter_session::_filter_session(void)
{
    session_count++;
    return;
}


_filter_session::~_filter_session(void)
{
    session_count--;
    return;
}

int main(int argc, const char **argv)
{
  return 0;
}

Note, I commented FILTER_SESSION to compile on g++/Linux, and also added a main and removed the this-> (as another member mentions, the variable is not a property of the object, but of the class. Think it as a namespaced global variable)


Just use session_count++. A static variable is not tied to any instance of a class, and hence it cannot be accessed through the this-pointer. All instances of your class share one instance of session_count. In fact, session_count can be accessed even if no instances of your class exist.

Edit Ok, my answer does not solve the problem, but Charles Bailey`s does.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜