By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
435,144 Members | 1,056 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 435,144 IT Pros & Developers. It's quick & easy.

private static member access by a friend class

P: n/a
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

Feb 27 '06 #1
Share this Question
Share on Google+
2 Replies


P: n/a
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.

Feb 27 '06 #2

P: n/a
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
Feb 27 '06 #3

This discussion thread is closed

Replies have been disabled for this discussion.