friend cannot access private variable

G

Gary Wessle

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
 
S

Sumit RAJAN

Gary said:
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.
 
I

I V

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.
 
S

sarathy

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
 

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,777
Messages
2,569,604
Members
45,203
Latest member
KaliShumat

Latest Threads

Top