473,463 Members | 1,512 Online
Bytes | Software Development & Data Engineering Community
Create Post

Home Posts Topics Members FAQ

friend cannot access private variable

Hi

I am trying to call a function of a class which declares an object of
another class and return its private member.
thanks

************************************************** **************
int main(){
strategy1 m;
cout << m.get_balance() << endl;
}

****************
class strategy1 //interface
{
public:
double get_balance();
strategy1();
};

****************//implementation
strategy1::strategy1(){
cout << "strategy1 called" << endl;
}

double strategy1::get_balance(){
Account a;
return a.balance;
}

****************
class Account //interface
{
class strategy1;
friend class strategy1;

double balance; //<<--error line no. 12

public:
....
};

the error I am getting is

**************** error ****************
Account.h:12: error: 'double Account::balance' is private
Aug 9 '06 #1
5 5298
Gary Wessle wrote:
Hi

I am trying to call a function of a class which declares an object of
another class and return its private member.
thanks

************************************************** **************
int main(){
strategy1 m;
cout << m.get_balance() << endl;
}

****************
class strategy1 //interface
{
public:
double get_balance();
strategy1();
};

****************//implementation
strategy1::strategy1(){
cout << "strategy1 called" << endl;
}

double strategy1::get_balance(){
Account a;
return a.balance;
}

****************
class Account //interface
{
class strategy1;
friend class strategy1;

double balance; //<<--error line no. 12

public:
...
};

the error I am getting is

**************** error ****************
Account.h:12: error: 'double Account::balance' is private

#include <iostream>

class strategy1;

class Account //interface
{
friend class strategy1;
double balance;

public:
Account() : balance(0.0) {}
};

class strategy1 //interface
{
public:
double get_balance();
strategy1();
};
strategy1::strategy1(){
std::cout << "strategy1 called" << std::endl;
}

double strategy1::get_balance(){
Account a;
return a.balance;
}


int main(){
strategy1 m;
std::cout << m.get_balance() << std::endl;
}

Note that I have added a constructor for Account. I have also rearranged
some of you definitions.

Regards,
Sumit.
Aug 9 '06 #2
Sumit RAJAN wrote:
>
#include <iostream>

class strategy1;
Also note that the above forward declaration is not really required.

Sumit.
Aug 9 '06 #3
I V
On Wed, 09 Aug 2006 15:30:44 +1000, Gary Wessle wrote:
I am trying to call a function of a class which declares an object of
another class and return its private member.
[...]
****************
class Account //interface
{
class strategy1;
This declares a class Account::strategy1, which shadows the class
::strategy1 .
friend class strategy1;
This makes the class Account::strategy1 a friend of Account. ::strategy1
is _not_ made a friend here.
[...]
Account.h:12: error: 'double Account::balance' is private
Because the friend class here is Account::strategy1, not ::strategy1 .
Move the forward declaration of strategy1 outside of the definition of
Account, and it should compile.
Aug 9 '06 #4
Sumit RAJAN <su*********@gmail.comwrites:
Sumit RAJAN wrote:
>>
#include <iostream>

class strategy1;

Also note that the above forward declaration is not really required.

Sumit.
thank you
Aug 9 '06 #5
Hi,
class Account //interface
{
class strategy1;
friend class strategy1;

double balance; //<<--error line no. 12
I am not sure why you need the first declaration. "class strategy1;"
I think the second one should do fine.

Correct me if i am wrong.

Regards,
Sarathy

Aug 9 '06 #6

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

10
by: Der Andere | last post by:
I've got a problem concerning the << operator. Although I've declared it as a friend function it cannot access the private member of the class AVLtree. Some code: ------------------ class...
6
by: Adam Parkin | last post by:
Hello, all I'm having a problem with friend functions in a templatized Queue class I'm writing using linked lists. The problem is that I can't get the friend function to be able to access private...
5
by: Srini nandiraju | last post by:
Hi folks, I am trying to implement a friend class and the following is what I did. Please scroll down. /***************** CODE **********************************/ class BKP { private: int...
4
by: Gactimus | last post by:
Hey again, I am working my way through the book "C++ Primer Fourth Edition". I am trying to run a program out of the book using Microsoft Visual C++ version 6 and I am given the error message:...
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...
3
by: hartley_aaron | last post by:
Hi, What does it mean when VS automatically adds a line like this to code of my form: Friend WithEvents Label1 As System.Windows.Forms.Label Also, I noticed a modifier property for each...
6
by: Philip Potter | last post by:
I have a graph data structure. It goes something like this: class GraphNode { private: friend class Graph; // successor GraphNodes stored as indices of the Graph's array int value; int next1,...
6
by: Sunny | last post by:
Hi All, In C++ , if a function or class is declared friend, then where is that information stored? How does friend function get access to private data of a Class? Thanks
2
by: Immortal Nephi | last post by:
I design a class for general purpose. I do not allow a client to read or modify interface and implemention. I allow them to write a new non- member function outside of class' interface and...
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 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 a new...
0
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The...
0
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated ...

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.