C++Talk.NET Forum Index C++Talk.NET
C++ language newsgroups
 
Archives   FAQFAQ   SearchSearch   MemberlistMemberlist   UsergroupsUsergroups   RegisterRegister 
 ProfileProfile   Log in to check your private messagesLog in to check your private messages   Log inLog in 

private static member access by a friend class

 
Post new topic   Reply to topic    C++Talk.NET Forum Index -> C++ language (comp.lang.c++)
View previous topic :: View next topic  
Author Message
Anjo Gasa
Guest





PostPosted: Mon Feb 27, 2006 6:06 pm    Post subject: private static member access by a friend class Reply with quote



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
Back to top
Rolf Magnus
Guest





PostPosted: Mon Feb 27, 2006 6:06 pm    Post subject: Re: private static member access by a friend class Reply with quote



Anjo Gasa wrote:

Quote:
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.

Quote:
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.

Quote:
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.
Back to top
Victor Bazarov
Guest





PostPosted: Mon Feb 27, 2006 6:06 pm    Post subject: Re: private static member access by a friend class Reply with quote



Anjo Gasa wrote:
Quote:
I have two classes:

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

That's a declaration. Where is it defined?

Quote:
}
;

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

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

B::rate = 5.0f;

Quote:
}
;

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.

Quote:
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
Back to top
Display posts from previous:   
Post new topic   Reply to topic    C++Talk.NET Forum Index -> C++ language (comp.lang.c++) All times are GMT
Page 1 of 1

 
Jump to:  
You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot vote in polls in this forum


Powered by phpBB © 2001, 2006 phpBB Group
SEO toolkit © 2004-2006 webmedic.