c++ - Friend Classes: Cannot Access private members? -


i learning c++ friend classes. says on books , tuts, friend class can access members (private , protected) too. not happen in case.

i know there's stupid error cannot see. please me find :d

my code:

#include <iostream> using namespace std;  class a; class b { private:     int num; public:     b(int n=0):num(n){}      friend int add(a, b);     friend int mul(a, b);     friend int sub(a, b);     void showthis(a);     friend class a; };  class a{ private:     int num; public:     a(int n=0):num(n){}      friend int add(a, b);     friend int mul(a, b);     friend int sub(a, b);    };  int add(a a, b b){     return a.num+b.num; }  int sub(a a, b b){     return a.num-b.num; }  int mul(a a, b b){     return a.num*b.num; }   void b::showthis(a a){     cout<<a.num<<endl; }  int main(){     a(3);     b b(6);     cout<<add(a,b)<<endl;     cout<<mul(a,b)<<endl;     cout<<sub(a,b)<<endl;     b.showthis(a); } 

the error:

q17.cpp: in member function ‘void b::showthis(a)’: q17.cpp:20:6: error: ‘int a::num’ private   int num;       ^ q17.cpp:43:10: error: within context   cout<<a.num<<endl; 

you declared neither b::showthis(a) nor class b class a's friend.

you add either

friend b::showthis(a); 

or

friend class b; 

into class a.


Comments

Popular posts from this blog

python - Subclassed QStyledItemDelegate ignores Stylesheet -

java - HttpClient 3.1 Connection pooling vs HttpClient 4.3.2 -

SQL: Divide the sum of values in one table with the count of rows in another -