Friend can't see private members?

Hello!
Please, function mmm is ment to be a friend function. So, it shoudl be able to see a and b, even though they are both privete, isnt it?

Please, just checking where I made mistake, can someone see it faster then me? many thank!!

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
  #include <iostream>
    using namespace std;
     
    class First{
     
    int a;
    int b;
     
    public:
    First(int c,int d):a(c), b(d){};
     
    int sum(){ return (a+b); }
    friend int mmm(int, int) ;
    };
     
    int mmm(int x, int y) {
    int z=x+y;
    return z;
    }
     
     
    int main(){
     
    First sum1(2,3);
    First sum2(4,5);
     
    cout<<sum1.sum()<<endl;
    cout<<sum2.sum()<<endl;
     
     
    int r;
    
  
    r = mmm(sum1.a, sum1.b);
     
     
    cout<< r <<endl;
     
    return 0;
    }
mmm is a friend, but main isn't. This way would work:

1
2
3
4
5
6
7
8
9
10
int mmm(First f)
{
    return f.a + f.b;
}

int main()
{
    First sum1(2, 3);
    int r = mmm(sum1);
}
Hello!
did I understand it right? This code still does not work:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
#include <iostream>
    using namespace std;
     
    class First{
     
    int a;
    int b;
     
    public:
    First(int c,int d):a(c), b(d){};
     
    int sum(){ return (a+b); }
    friend int mmm(int, int) ;
    };
     
    int mmm(First f) {
    return f.a+f.b
    }
     
     
    int main(){
     
    First sum1(2,3);
    First sum2(4,5);
     
    cout<<sum1.sum()<<endl;
    cout<<sum2.sum()<<endl;
     
     
    int r;
    
  
    r = mmm(sum1);
     
     
    cout<< r <<endl;
     
    return 0;
    }
 



Solved! Done! MANY THANKS!!!
Topic archived. No new replies allowed.