Mail Archives: djgpp/2001/05/03/13:26:01
Rafal Maj wrote:
>
> (Please, I need to get answer fast - if You can help me)
> I didn't use static for long time, so I have totaly forgoten how to use it
> :-(
>
> Why this code :
> class cA {
> private : static int a;
> public : static void f();
> };
> void cA::f() { a=3; }
> int main() { }
> gives me this error message :
> Creating: a.exe
> Error: a.o: In function `cA::f(void)':
> Error: a.cc(.text+0x5): undefined reference to `cA::a'
> Error: collect2: ld returned 1 exit status
Of course. A static class variable needs to be defined. You need
to add
int cA::a = 0;
somewhere in your program.
> Why after moving body of static function inside class definition everything
> is allright :
> class cA {
> private : static int a;
> public : static void f() { a=3; }; // ok
> };
> // void cA::f() { a=3; } - error
> int main() { }
Because cA::f() is now an inline function. And because it is never
called,
it is never expanded, so its use of cA::a is never recorded as a
dependency.
> How to write this small example correct ? I must have funciton body outside
> of class definition, because I want this class to be splited into .cc and .h
Simply put
int cA::a = 0;
into the .cc file
--
Tim Van Holder - Falcon Software N.V.
=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=
This message was posted using plain text. I do not endorse any
products or services that may be hyperlinked to this message.
- Raw text -