On 4 Feb 2004 00:45:00 -0800,
(sytee) wrote:
>i have the following and it compile no problem
>
>example1:
>-------------------------------------------------------------------------------
>class Giant{
>public:
> Giant();
> int getHeight(){ return height; }
>
>private:
> static int height;
>};
This shouldn't really compile, because you have only declared 'height'.
In addition to being declared, it needs to be defined somewhere, in some
compilation unit. The definition looks like
int Giant::height = 0;
and in effect it reserves memory for the variable and defines an initial
value (which will be 0 if no initial value is specified).
The reason that it seems to compile OK without a definition might be that
you never actually use the Giant class.
When the class is used Microsoft Visual C++ 7.1 gives this error messaqe:
error LNK2019: unresolved external symbol "public: __thiscall Giant::Giant(void)"
(??0Giant@@QAE@XZ) referenced in function _main
>-------------------------------------------------------------------------------
>
>i try to change the style of example1 to
>
>example2
>-------------------------------------------------------------------------------
>class Giant{
>public:
> Giant();
> int getHeight(); // here we change
>
>private:
> static int height;
>};
>
>int Giant::getHeight() {return height;}
>-------------------------------------------------------------------------------
>
>there will be error message saying that:
>test.obj : error LNK2001: unresolved external symbol "private: static
>int Giant::height" (?height@Giant@@0HA)
>
>
>how can i solve the problem by using back the example2 style?
See above.