Board index » delphi » C++ 5.01 Question: static variables in classes

C++ 5.01 Question: static variables in classes

Hi, can anyone help me with C++?

I tried to set the value of a static variable in a classfunction, but I get the error:unresolved external.

class test
{public:
 static int x;
 int abc(int y)
 { x=y; };

Quote
};

Why is this not allowed?
 

Re:C++ 5.01 Question: static variables in classes


Quote
"Mark" <ana...@gmx.net> wrote in message news:397f87dc$1_1@dnews...

> Hi, can anyone help me with C++?

> I tried to set the value of a static variable in a classfunction, but I

get the error:unresolved external.
Quote

> class test
> {public:
>  static int x;
>  int abc(int y)
>  { x=y; };
> };

> Why is this not allowed?

It is.  What you've described is a linker error, not a compilation error.

The linker error is due to your program containing no definition for the
static variable 'test::x'.

The following line is missing from your above code:

   int test::x;

Note that you can also optionally initialize a class static at the point of
definition:

   int test::x = 123;

- Matt

Note: My use of a bogus e-mail address is an anti-spam tactic.  To obtain my
valid address, replace "biteme" with "motu".  Sorry for the inconvenience.

Other Threads