problem in accessing static member variable

Discussion in 'C++' started by sytee, Feb 4, 2004.

  1. sytee

    sytee Guest

    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!!!
     
    sytee, Feb 4, 2004
    #1
    1. Advertising

  2. 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.
     
    Alf P. Steinbach, Feb 4, 2004
    #2
    1. Advertising

  3. sytee

    David Harmon Guest

    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/
     
    David Harmon, Feb 4, 2004
    #3
    1. Advertising

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

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. 0to60
    Replies:
    4
    Views:
    543
    jeffc
    Nov 21, 2003
  2. Sam
    Replies:
    4
    Views:
    491
    The Directive
    Jan 13, 2004
  3. Siemel Naran
    Replies:
    4
    Views:
    837
    Micah Cowan
    Jan 12, 2005
  4. aling
    Replies:
    6
    Views:
    505
    Xiaobin.Huang
    Oct 30, 2005
  5. dolphin
    Replies:
    3
    Views:
    1,396
    Pete Becker
    Dec 5, 2007
Loading...

Share This Page