473,320 Members | 1,856 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,320 software developers and data experts.

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!!!
Jul 22 '05 #1
2 1091
On 4 Feb 2004 00:45:00 -0800, s_*********@yahoo.com (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.

Jul 22 '05 #2
On 4 Feb 2004 00:45:00 -0800 in comp.lang.c++, s_*********@yahoo.com
(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/
Jul 22 '05 #3

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

5
by: chandra sekhar | last post by:
Hi All, I am trying to allocate memory for member variable in a static method belongs to same class.. the allocation is o.k and when try to access the member variable I find that it is a null...
16
by: Eric | last post by:
I have a static class member variable as follows: struct A { static void Set (int i) { v = i; } static int& Get () { return v; } static int v; }; int A::v; // define A::v in the cpp file
3
by: debadeepti | last post by:
Hi All, I am porting an application from watcom to vc compiler. In watcom and other compiler (other then vc) memory allocation of __vfptr inside class object are different. In vc __vfptr is...
11
by: ajay.sonawane | last post by:
Hello ther I read somewhere that callback function should be global or static member function. 1: I could use static member functions but how could I access members of class which ar not static....
7
by: ank | last post by:
Hi, I was curious about how to define static data member of template class. Should I put the definition in a separate source file or in the same header file as its template class? And when this...
0
by: rain_c1 | last post by:
hi! is there a way to access a static member of a class, if the classname is a variable? this does not work: $classname::$member for methods there is a solution:...
5
by: John Goche | last post by:
Hello, I would like to know whethere there is a difference between a const variable and a static const variable inside a class. After all, if a variable is const in a class, the compiler can...
12
by: titan nyquist | last post by:
I have a class with data and methods that use it. Everything is contained perfectly THE PROBLEM: A separate thread has to call a method in the current instantiation of this class. There is...
13
by: yanlinlin82 | last post by:
I'd like to write all code of a class in only header file. But when there is any static member varia ble, I have to define it in cpp file, not the header file. Otherwise, the static member variable...
0
by: DolphinDB | last post by:
Tired of spending countless mintues downsampling your data? Look no further! In this article, you’ll learn how to efficiently downsample 6.48 billion high-frequency records to 61 million...
0
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
1
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
0
by: Vimpel783 | last post by:
Hello! Guys, I found this code on the Internet, but I need to modify it a little. It works well, the problem is this: Data is sent from only one cell, in this case B5, but it is necessary that data...
0
by: jfyes | last post by:
As a hardware engineer, after seeing that CEIWEI recently released a new tool for Modbus RTU Over TCP/UDP filtering and monitoring, I actively went to its official website to take a look. It turned...
0
by: Defcon1945 | last post by:
I'm trying to learn Python using Pycharm but import shutil doesn't work
1
by: Shællîpôpï 09 | last post by:
If u are using a keypad phone, how do u turn on JavaScript, to access features like WhatsApp, Facebook, Instagram....
0
by: af34tf | last post by:
Hi Guys, I have a domain whose name is BytesLimited.com, and I want to sell it. Does anyone know about platforms that allow me to list my domain in auction for free. Thank you
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome former...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.