473,320 Members | 1,978 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,320 software developers and data experts.

private static member access by a friend class

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
2 3116
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
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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

3
by: Rajesh Garg | last post by:
Can we have private constructors and destructors? IF yes what is the use of such constructors or destructors.....in the sense where can these be implemented in a system................. I have...
7
by: Wolfgang Jeltsch | last post by:
Hello, I want to write a list class with an iterator class as an inner class. The iterator class must have access to certain private members of the list class in order to do its job. Here is a...
34
by: Andy | last post by:
1) Is there any use of defining a class with a single constructor declared in private scope? I am not asking a about private copy constructors to always force pass/return by reference. 2) Is...
12
by: Bryan Parkoff | last post by:
CMain Class is the base class that is initialized in main function. CA Class is the base class that is initialized in CMain::CMain(). CMain Class is always public while CA Class is always...
5
by: JustSomeGuy | last post by:
I have a class with a private data member and I want to access it from a friend function, but Visual Studio 2003 .NET won't let me. class MyClass { private: static int level; public: friend...
15
by: Samee Zahur | last post by:
Question: How do friend functions and static member functions differ in terms of functionality? I mean, neither necessarily needs an object of the class to be created before they are called and...
18
by: Gernot Frisch | last post by:
This code works on VC7.1. But VC6 refuses to compile. class foo { class bar { friend class foo; foo & m_f; public: bar(foo & f) : m_f(f) {}
11
by: Yarco | last post by:
For example: <?php class Test { private $name = 'yarco'; } $p = new ReflectionPropery('Test', 'name'); print $p->getValue();
5
by: Anonymous | last post by:
I have a class that needs to be accesed by a C API. I need to expose some private methods to the C API : #ifdef __cplusplus extern "C" #endif void peek(Object_Handle handle); void...
0
by: DolphinDB | last post by:
Tired of spending countless mintues downsampling your data? Look no further! In this article, you’ll learn how to efficiently downsample 6.48 billion high-frequency records to 61 million...
0
by: ryjfgjl | last post by:
ExcelToDatabase: batch import excel into database automatically...
0
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
0
by: jfyes | last post by:
As a hardware engineer, after seeing that CEIWEI recently released a new tool for Modbus RTU Over TCP/UDP filtering and monitoring, I actively went to its official website to take a look. It turned...
1
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
1
by: Defcon1945 | last post by:
I'm trying to learn Python using Pycharm but import shutil doesn't work
1
by: Shællîpôpï 09 | last post by:
If u are using a keypad phone, how do u turn on JavaScript, to access features like WhatsApp, Facebook, Instagram....
0
by: af34tf | last post by:
Hi Guys, I have a domain whose name is BytesLimited.com, and I want to sell it. Does anyone know about platforms that allow me to list my domain in auction for free. Thank you
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome former...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.