private static member access by a friend class

A

Anjo Gasa

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
 
V

Victor Bazarov

Anjo said:
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
 
R

Rolf Magnus

Anjo said:
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.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,995
Messages
2,570,233
Members
46,820
Latest member
GilbertoA5

Latest Threads

Top