473,786 Members | 2,705 Online
Bytes | Software Development & Data Engineering Community
+ 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::stra tegy1(){
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::balanc e' is private
Aug 9 '06 #1
5 5313
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::stra tegy1(){
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::balanc e' 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::stra tegy1(){
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::strate gy1, which shadows the class
::strategy1 .
friend class strategy1;
This makes the class Account::strate gy1 a friend of Account. ::strategy1
is _not_ made a friend here.
[...]
Account.h:12: error: 'double Account::balanc e' is private
Because the friend class here is Account::strate gy1, 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*********@gm ail.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
4344
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 AVLtree { public: friend ostream& operator << (ostream& os, AVLtree& t){}; .... some unimportant stuff ...
6
2340
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 data from the class. Here's the gist of the code: template <class T> struct NodeType { T data; NodeType<T> * link;
5
2451
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 nVar;
4
2419
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: : error C2248: 'hours' : cannot access private member declared in class 'Time' : error C2248: 'minutes' : cannot access private member declared in class 'Time'
15
6594
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 either has access only to static members of the class (ie. assuming no object of the class is in scope - neither by arguments recieved nor by local declarations). Any static member function like this: //accessing static member i static void...
3
27395
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 control and the default is Friend. When modifier is set to Friend, I can access the control from anywhere with the application, while if it is set to private it can
6
5555
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, next2; };
6
1558
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
2432
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 implmention. The problem is that non-member function cannot access private data member. The friend keyword is not the solution. I am aware that friend keyword allows the non-member function to access private data member. If you define more than...
0
9647
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
9496
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 effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
9961
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 protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
1
7512
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 presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
6745
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
5397
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 last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
0
5534
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
4066
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 we have to send another system
2
3669
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.

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.