Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > C++ > problem in accessing static member variable

Reply
Thread Tools

problem in accessing static member variable

 
 
sytee
Guest
Posts: n/a
 
      02-04-2004
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!!!
 
Reply With Quote
 
 
 
 
Alf P. Steinbach
Guest
Posts: n/a
 
      02-04-2004
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.

 
Reply With Quote
 
 
 
 
David Harmon
Guest
Posts: n/a
 
      02-04-2004
On 4 Feb 2004 00:45:00 -0800 in comp.lang.c++,
(sytee) was alleged to have written:
>there will be error message saying that:
>test.obj : error LNK2001: unresolved external symbol "private: static
>int Giant::height" (?height@Giant@@0HA)


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/


 
Reply With Quote
 
 
 
Reply

Thread Tools

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 On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Can a static member function access non-static member? dolphin C++ 3 12-05-2007 12:39 PM
Accessing a protected member of a member of type BaseClass???? Steven T. Hatton C++ 2 08-16-2004 03:11 PM
How would I use qsort to sort a struct with a char* member and a long member - I want to sort in order of the long member Angus Comber C Programming 7 02-05-2004 06:41 PM
Does a static variable in a class's member fn always remain static? Sam C++ 4 01-13-2004 11:05 PM
performance of static member function vs. instance member function 0to60 C++ 4 11-21-2003 05:25 PM



Advertisments
 



1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57