Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > C++ > private static member access by a friend class

Reply
Thread Tools

private static member access by a friend class

 
 
Anjo Gasa
Guest
Posts: n/a
 
      02-27-2006
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

 
Reply With Quote
 
 
 
 
Victor Bazarov
Guest
Posts: n/a
 
      02-27-2006
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
 
Reply With Quote
 
 
 
 
Rolf Magnus
Guest
Posts: n/a
 
      02-27-2006
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.

 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Can a static member function access non-static member? dolphin C++ 3 12-05-2007 12:39 PM
how to access a private member function without using friend and virtual rajasekaran.psg@gmail.com C++ 2 01-05-2007 10:07 AM
Can a sub-class (composite object) member function access private or protected members of base class? pkpatil@gmail.com C++ 2 06-09-2006 03:09 PM
Can Derived class static member access protected member from base class? Siemel Naran C++ 4 01-12-2005 06:46 PM
Can't access a static private data member from a friend function? JustSomeGuy C++ 5 12-05-2004 05:51 PM



Advertisments