Static class member Linker Errors

Discussion in 'C++' started by hyling.s1@gmail.com, Aug 9, 2006.

  1. Guest

    Hi I'm getting "unresolved external symbol" linker errors from VC++
    2003 when I use a static instance of a class for example

    class myClass
    {
    protected:
    static class2 mInstance; /* fails with "unresolved external symbol" */
    static int mVar; /* works fine if I init it in the .cpp with: int
    myClass:mVar = 0; */
    }

    What am I doing wrong?

    Thanks
    Hua-Ying
    , Aug 9, 2006
    #1
    1. Advertising

  2. posted:

    > Hi I'm getting "unresolved external symbol" linker errors from VC++
    > 2003 when I use a static instance of a class for example
    >
    > class myClass
    > {
    > protected:
    > static class2 mInstance; /* fails with "unresolved external symbol" */
    > static int mVar; /* works fine if I init it in the .cpp with: int
    > myClass:mVar = 0; */
    > }



    class myClass {
    protected:

    class2 static mInstance; /* Just a declaration */
    };

    class2 myClass::mInstance( /* Arguments */ ); /* Definition */

    --

    Frederick Gotham
    Frederick Gotham, Aug 9, 2006
    #2
    1. Advertising

  3. wrote:
    > Hi I'm getting "unresolved external symbol" linker errors from VC++
    > 2003 when I use a static instance of a class for example
    >
    > class myClass
    > {
    > protected:
    > static class2 mInstance; /* fails with "unresolved external symbol" */
    > static int mVar; /* works fine if I init it in the .cpp with: int
    > myClass:mVar = 0; */
    > }
    >
    > What am I doing wrong?


    You're probably not defining your 'mInstance' anywhere. Read up on
    static data members.

    V
    --
    Please remove capital 'A's when replying by e-mail
    I do not respond to top-posted replies, please don't ask
    Victor Bazarov, Aug 9, 2006
    #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. E11
    Replies:
    1
    Views:
    4,686
    Thomas Weidenfeller
    Oct 12, 2005
  2. Serge
    Replies:
    4
    Views:
    8,673
    Paavo Helde
    Dec 19, 2004
  3. Siemel Naran
    Replies:
    4
    Views:
    780
    Micah Cowan
    Jan 12, 2005
  4. dolphin
    Replies:
    3
    Views:
    1,319
    Pete Becker
    Dec 5, 2007
  5. Philipp Kraus

    static member linker problem

    Philipp Kraus, Oct 11, 2011, in forum: C++
    Replies:
    6
    Views:
    367
    Victor Bazarov
    Oct 11, 2011
Loading...

Share This Page