www.delorie.com/archives/browse.cgi   search  
Mail Archives: djgpp/2001/05/03/13:26:01

Message-ID: <3AF175FE.81CC4439@falconsoft.be>
Date: Thu, 03 May 2001 17:15:10 +0200
From: Tim Van Holder <tim DOT vanholder AT falconsoft DOT be>
Organization: Falcon Software NV
X-Mailer: Mozilla 4.77 [en] (X11; U; Linux 2.2.16-3 i686)
X-Accept-Language: en, nl-BE, nl
MIME-Version: 1.0
Newsgroups: comp.os.msdos.djgpp
Subject: Re: static
References: <9cre3n$4bq$1 AT info DOT cyf-kr DOT edu DOT pl>
Lines: 54
NNTP-Posting-Host: 194.78.64.238
X-Trace: 988902880 reader0.news.skynet.be 3122 194.78.64.238
X-Complaints-To: abuse AT skynet DOT be
To: djgpp AT delorie DOT com
DJ-Gateway: from newsgroup comp.os.msdos.djgpp
Reply-To: djgpp AT delorie DOT com

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 -


  webmaster     delorie software   privacy  
  Copyright © 2019   by DJ Delorie     Updated Jul 2019