problem in accessing static member variable

S

sytee

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!!!
 
A

Alf P. Steinbach

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.
 
D

David Harmon

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/
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,769
Messages
2,569,580
Members
45,054
Latest member
TrimKetoBoost

Latest Threads

Top