C++Talk.NET Forum Index C++Talk.NET
C++ language newsgroups
 
Archives   FAQFAQ   SearchSearch   MemberlistMemberlist   UsergroupsUsergroups   RegisterRegister 
 ProfileProfile   Log in to check your private messagesLog in to check your private messages   Log inLog in 

friend cannot access private variable

 
Post new topic   Reply to topic    C++Talk.NET Forum Index -> C++ language (comp.lang.c++)
View previous topic :: View next topic  
Author Message
Gary Wessle
Guest





PostPosted: Wed Aug 09, 2006 9:10 am    Post subject: friend cannot access private variable Reply with quote



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
Back to top
Sumit RAJAN
Guest





PostPosted: Wed Aug 09, 2006 9:10 am    Post subject: Re: friend cannot access private variable Reply with quote



Gary Wessle wrote:
Quote:
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.
Back to top
Gary Wessle
Guest





PostPosted: Wed Aug 09, 2006 9:10 am    Post subject: Re: friend cannot access private variable Reply with quote



Sumit RAJAN <sumit.rajan (AT) gmail (DOT) com> writes:

Quote:
Sumit RAJAN wrote:


#include <iostream

class strategy1;

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

Sumit.

thank you
Back to top
I V
Guest





PostPosted: Wed Aug 09, 2006 9:10 am    Post subject: Re: friend cannot access private variable Reply with quote

On Wed, 09 Aug 2006 15:30:44 +1000, Gary Wessle wrote:
Quote:
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 .

Quote:
friend class strategy1;

This makes the class Account::strategy1 a friend of Account. ::strategy1
is _not_ made a friend here.

Quote:
[...]

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.
Back to top
Sumit RAJAN
Guest





PostPosted: Wed Aug 09, 2006 9:10 am    Post subject: Re: friend cannot access private variable Reply with quote

Sumit RAJAN wrote:

Quote:

#include <iostream

class strategy1;

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

Sumit.
Back to top
Post new topic   Reply to topic    C++Talk.NET Forum Index -> C++ language (comp.lang.c++) All times are GMT
Page 1 of 1

 
 


Powered by phpBB © 2001, 2006 phpBB Group