a static member variable error 
Author Message
 a static member variable error

Hi, all:
I wrote a program which used static member variable as follows:
class A
{
public:
 static int k;
 A(){
  k = 0;
 }

Quote:
};

void main()
{
 A::k++;
 return;

Quote:
}

but link reported: error LNK2001: unresolved external symbol "public: static

anyone could help me how to fix this error. Thanks a lot.
win


Fri, 05 Dec 2003 13:39:08 GMT  
 a static member variable error
    You have only declared the static variable; you have not defined it.

    Someplace in you program you must include:

    int    A::k;

--
Truth,
James Curran
www.NJTheater.com     (Professional)
www.NovelTheory.com  (Personal)
www.BrandsForLess.Com (Day Job)


Quote:
> Hi, all:
> I wrote a program which used static member variable as follows:
> class A
> {
> public:
>  static int k;
>  A(){
>   k = 0;
>  }
> };

> void main()
> {
>  A::k++;
>  return;
> }

> but link reported: error LNK2001: unresolved external symbol "public:
static

> anyone could help me how to fix this error. Thanks a lot.
> win



Fri, 05 Dec 2003 10:41:04 GMT  
 a static member variable error
As a followup...

Static class member variables must have an "instance" or must have
their memory initialized once somewhere. You cannot do this in the
header file for the class because chances are you will be including
this header file multiple times. I usually do it in the front of the
.cpp file for the class. And, I initialize the variable with a value.

int A::k = 0;

On Sun, 17 Jun 2001 22:41:04 -0400, "James Curran"

Quote:

>    You have only declared the static variable; you have not defined it.

>    Someplace in you program you must include:

>    int    A::k;



Sat, 06 Dec 2003 07:06:31 GMT  
 
 [ 3 post ] 

 Relevant Pages 

1. C2086 error: static local variables in member functions

2. Getting a Linking error when using a static member variable in a DLL

3. static function access member variable and member function

4. static member variables

5. static member variable inside class

6. LNK1169 & static const class member variables

7. LNK2001 on static member variables

8. Export static member variables in DLL

9. virtual static member variable

10. write to static class member variable...

11. static variable defined in a member function

12. Static member variables of template class

 

 
Powered by phpBB® Forum Software