Connecting Tech Pros Worldwide Help | Site Map

problem in accessing static member variable

 
LinkBack Thread Tools Search this Thread
  #1  
Old July 22nd, 2005, 06:07 AM
sytee
Guest
 
Posts: n/a
Default problem in accessing static member variable

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, 06:07 AM
Alf P. Steinbach
Guest
 
Posts: n/a
Default 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, 06:07 AM
David Harmon
Guest
 
Posts: n/a
Default 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/


 

Bookmarks

Thread Tools Search this Thread
Search this Thread:

Advanced Search

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On

Popular Articles

What is Bytes?

We are a network of experts and professionals in IT and software development that help one another with answers to tough questions and share insights. Get the best answers to your questions from over 220,840 network members.