private static member access by a friend class

Discussion in 'C++' started by Anjo Gasa, Feb 27, 2006.

  1. Anjo Gasa

    Anjo Gasa Guest

    I have two classes:

    // B.h
    class B
    {
    friend class A;
    //...
    private:
    static float rate;
    }

    // A.h
    class A
    {
    //...
    bool Init();
    //...
    }

    // A.cpp
    bool A:Init()
    {
    //...
    B:rate = 5.0;
    }

    A.cpp comiles fine, I get an error with the linker:

    error LNK2001: unresolved external symbol "private: static float rate"

    Now, I've tried to find something related to this situation via Google
    and I'm rifling through TC++PL, and have yet to find anything that can
    explain why there is a linking error.

    Anjo
    Anjo Gasa, Feb 27, 2006
    #1
    1. Advertising

  2. Anjo Gasa wrote:
    > I have two classes:
    >
    > // B.h
    > class B
    > {
    > friend class A;
    > //...
    > private:
    > static float rate;


    That's a declaration. Where is it defined?

    > }

    ;
    >
    > // A.h
    > class A
    > {
    > //...
    > bool Init();
    > //...
    > }

    ;
    >
    > // A.cpp
    > bool A:Init()
    > {
    > //...
    > B:rate = 5.0;


    B::rate = 5.0f;

    > }

    ;
    >
    > A.cpp comiles fine, I get an error with the linker:
    >
    > error LNK2001: unresolved external symbol "private: static float rate"


    Of course. You didn't define 'B::rate'. Read your favourite C++ book
    about static data members, especially the part about how they need to be
    defined somewhere.

    > Now, I've tried to find something related to this situation via Google
    > and I'm rifling through TC++PL, and have yet to find anything that can
    > explain why there is a linking error.


    Well, now you know.

    V
    --
    Please remove capital As from my address when replying by mail
    Victor Bazarov, Feb 27, 2006
    #2
    1. Advertising

  3. Anjo Gasa

    Rolf Magnus Guest

    Anjo Gasa wrote:

    > I have two classes:
    >
    > // B.h
    > class B
    > {
    > friend class A;
    > //...
    > private:
    > static float rate;
    > }
    >
    > // A.h
    > class A
    > {
    > //...
    > bool Init();
    > //...
    > }
    >
    > // A.cpp
    > bool A:Init()
    > {
    > //...
    > B:rate = 5.0;
    > }
    >
    > A.cpp comiles fine,


    That's strange. Your code contains several errors.

    > I get an error with the linker:
    >
    > error LNK2001: unresolved external symbol "private: static float rate"


    You only declared B::rate, but never defined it.

    > Now, I've tried to find something related to this situation via Google
    > and I'm rifling through TC++PL, and have yet to find anything that can
    > explain why there is a linking error.


    Look for "static member" in the index of your TC++PL.
    Rolf Magnus, Feb 27, 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. Bryan Parkoff
    Replies:
    12
    Views:
    934
    Bryan Parkoff
    Apr 12, 2004
  2. JustSomeGuy
    Replies:
    5
    Views:
    896
    JustSomeGuy
    Dec 5, 2004
  3. Siemel Naran
    Replies:
    4
    Views:
    803
    Micah Cowan
    Jan 12, 2005
  4. Replies:
    2
    Views:
    713
    Ondra Holub
    Jan 5, 2007
  5. dolphin
    Replies:
    3
    Views:
    1,342
    Pete Becker
    Dec 5, 2007
Loading...

Share This Page