Connecting Tech Pros Worldwide Help | Site Map

problem in accessing static member variable

  #1  
Old July 22nd, 2005, 07:07 AM
sytee
Guest
 
Posts: n/a
i have the following and it compile no problem

example1:
-------------------------------------------------------------------------------
class Giant{
public:
Giant();
int getHeight(){ return height; }

private:
static int height;
};
-------------------------------------------------------------------------------

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?

thank you very much!!!
  #2  
Old July 22nd, 2005, 07:07 AM
Alf P. Steinbach
Guest
 
Posts: n/a

re: problem in accessing static member variable


On 4 Feb 2004 00:45:00 -0800, s_yuan31tee@yahoo.com (sytee) wrote:
[color=blue]
>i have the following and it compile no problem
>
>example1:
>-------------------------------------------------------------------------------
>class Giant{
>public:
> Giant();
> int getHeight(){ return height; }
>
>private:
> static int height;
>};[/color]


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


[color=blue]
>-------------------------------------------------------------------------------
>
>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?[/color]

See above.

  #3  
Old July 22nd, 2005, 07:07 AM
David Harmon
Guest
 
Posts: n/a

re: problem in accessing static member variable


On 4 Feb 2004 00:45:00 -0800 in comp.lang.c++, s_yuan31tee@yahoo.com
(sytee) was alleged to have written:[color=blue]
>there will be error message saying that:
>test.obj : error LNK2001: unresolved external symbol "private: static
>int Giant::height" (?height@Giant@@0HA)[/color]

This issue is covered in Marshall Cline's C++ FAQ. See the topic
"[10.10] Why are classes with static data members getting linker
errors?" It is always good to check the FAQ before posting. You can
get the FAQ at:
http://www.parashift.com/c++-faq-lite/


Closed Thread


Similar Threads
Thread Thread Starter Forum Replies Last Post
Using an ActiveX in a Web service Oriane answers 4 July 29th, 2008 06:55 AM
How to access this static variable LamSoft answers 9 July 19th, 2007 04:55 PM
global variable vs static member initialization order n.torrey.pines@gmail.com answers 10 March 15th, 2007 03:35 PM
Static properties Mantorok answers 4 November 19th, 2005 08:14 AM