views:

196

answers:

4

Hi guys,

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

A: 

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.

Space_C0wb0y
All I want is to keep the count of number of active sessions... How do I do it then? if its globally accessible, anyone outside the class can access and change the variable!!!
Microkernel
@Microkernel: You've missed Space_C0wb0y's point. Inside the class `session_count` resolves to `_filter_session::session_count` anyway. Because it's a static variable `this->session_count`, `_filter_session::session_count` and `session_count` all refer to the same object. It wasn't suggested that you make the variable global.
Charles Bailey
The variable is still encapsulated within the class.
DeadMG
+7  A: 

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.

Charles Bailey
I think this has do be done in exactly on compilation unit, or am I mixing things up here. Doing this in a header would most likely result in multiply defined symbols.
Space_C0wb0y
Yes, it need to satisfy the ODR and be defined only once in the program, but as the constructor and destructor are also defined 'out of line' in the same block of code I was guessing that this wasn't a header or must only be included once anyway.
Charles Bailey
Yes, the fucntions are in a separate C++ file. Thanks for pointing out, had not thought abt it ;)
Microkernel
+1  A: 

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.

anon
+1  A: 

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)

duncan
So is that static variable accessible by non-member functions. If so, how to make sure that session_count holds number of active sessions?Thanks for the reply :)
Microkernel
No, it won't be accessible because it is declared private. The fact that you can initialize it that way does not mean you have access to it outside of the class.
duncan