write to static class member variable... 
Author Message
 write to static class member variable...

Hi all,
        I need to assign different values to a static variable of a
class. The following code do not work:

Set.h
class CSet
{
    public:
                 ......
                static int count ;

    protected:
                ........

Quote:
};

int CSet::count = 0; // Is this line neccessary??

CSetView.cpp

............

if ( type = = "DS")
CSet::count = 0;

.....

The error message i got was :
unresolved external symbol "public: static int  CSet::count"

can someone tell me how to write to a static member?? I try declaring a
static member function and do the assignment inside it....and the same
problem still arise....i also try using friend static function and it
still won't work. btw, i am using VC++ 6.0

pls e-mail me in addition to the newsgroup.
Thanks in advance....
leesze

--
********************************************************
*As Imagination Bodies Forth
*The Forms Of Things Unknown, The Poet's Pen
*Turns Them To Shapes, And Gives To Airy Nothing
*A Local Habitation And A Name
*
********************************************************



Fri, 28 Sep 2001 03:00:00 GMT  
 write to static class member variable...
Basically, you have to put the instantiation in the cpp file and not the
header.  Here is an example of a static count variable that counts how
many instances are created of a class...

-Brian

teststatic.h
--------------------
class TestStatic {
public:
  TestStatic();
  ~TestStatic();

  static int count;

Quote:
};

//int count=0;          // Cannot do this or you will get link errors

teststatic.cpp
--------------------
#include "TestStatic.h"

int TestStatic::count=0;// Here is where the static member goes

TestStatic::TestStatic() {
  count++;

Quote:
}

TestStatic::~TestStatic() {
  count--;

Quote:
}

main.cpp
--------------------
#include <stdio.h>
#include "TestStatic.h"

int main(int argc, char* argv[]) {
  TestStatic *ts = new TestStatic();
  printf("Count: %d\n",ts->count);

  TestStatic *TS = new TestStatic();
  printf("Count: %d\n",ts->count);
  printf("Count: %d\n",TS->count);

  delete ts;
  printf("Count: %d\n",TS->count);

  delete TS;

  return 0;

Quote:
}

--
Brian Rectanus

Quote:

> Hi all,
>         I need to assign different values to a static variable of a
> class. The following code do not work:

> Set.h
> class CSet
> {
>     public:
>                  ......
>                 static int count ;

>     protected:
>                 ........

> };
> int CSet::count = 0; // Is this line neccessary??

> CSetView.cpp

> ............

> if ( type = = "DS")
> CSet::count = 0;

> .....

> The error message i got was :
> unresolved external symbol "public: static int  CSet::count"

> can someone tell me how to write to a static member?? I try declaring a
> static member function and do the assignment inside it....and the same
> problem still arise....i also try using friend static function and it
> still won't work. btw, i am using VC++ 6.0

> pls e-mail me in addition to the newsgroup.
> Thanks in advance....
> leesze

> --
> ********************************************************
> *As Imagination Bodies Forth
> *The Forms Of Things Unknown, The Poet's Pen
> *Turns Them To Shapes, And Gives To Airy Nothing
> *A Local Habitation And A Name
> *
> ********************************************************



Fri, 28 Sep 2001 03:00:00 GMT  
 
 [ 2 post ] 

 Relevant Pages 

1. writing to static class member variable...

2. write to static class member variable....

3. static member variable inside class

4. LNK1169 & static const class member variables

5. Static member variables of template class

6. static template class member variable in DLL

7. static function access member variable and member function

8. does static class member change class size?

9. Class object as static member of another class

10. How to access a member variable of a class from other class

11. Classes as member variables of another class

12. Using a class as a member variable in the document class

 

 
Powered by phpBB® Forum Software