Good $daytime,

> Date: Tue, 19 Jan 1999 15:36:16 -0500
> From: Yasushi Shoji <[EMAIL PROTECTED]>
> To: [EMAIL PROTECTED]
> Subject: static variable in C++

> trying to make a class with a static variable so that all objects
> instantiated for that class can share one variable.
> # hope i'm on the right road...

> Question 1:
>   how can i initialize counter at (A) to 0?
>   i replaced (A) with
>       static int counter = 0;

Try this:

    class base {
    ...
    }

    int base::counter = 0;

    int
    main()
    {
    ...

>   and compiler said:
>   "ANSI C++ forbids in-class initialization 
>    of non-const static member `counter'"

> Question 2;
>   it seems that linker is not able to link object files from
>   the source code:

> bash$ g++ -Wall static.cpp
> /tmp/ccPYx1jJ.o: In function `base::get_counter(void)':
> /tmp/ccPYx1jJ.o(.base::gnu.linkonce.t.get_counter(void)+0x8): undefined reference to 
>`base::counter'

The same answer.  You _declare_ a static variable but then forgot to
_define_ it.  Initial value is optional, but definition itself isn't.

C++-go o muzukashiku nai.

  Regards,
  Willy.

--
"No easy hope or lies        | Vitaly "Willy the Pooh" Fedrushkov
 Shall bring us to our goal, | Information Technology Division
 But iron sacrifice          | Chelyabinsk State University
 Of Body, Will and Soul."    | mailto:[EMAIL PROTECTED]  +7 3512 156770
                   R.Kipling | http://www.csu.ac.ru/~willy  VVF1-RIPE

Reply via email to