why is that happening what's the difference? thanks in advance.

Hi everyone. why when passing an object of a specific class as parameter to a friend function of the same object I have access problem whereas when define that object inside that function (as local variable) there is no problem of accessing that object's members here is the code for both cases

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
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
//THE CASE IN WHICH PROBLEM OCCURS
#include <iostream>
using namespace std;
class book
{
int price;
public:
friend void modify();
book();
};

book::book()
{
cin>>price;
}

void modify(book s[])
{
	
int k=s[0].price;
for(int i=1;i<5;i++)
if(s[i].price<k)
k=s[i].price;
 cout<<k;
}

void main()
{
	book s[5];
modify(s);

}



//THE OK CASE

#include <iostream>
using namespace std;
class book
{
int price;
public:
friend void modify();
book();
};

book::book()
{
cin>>price;
}

void modify()
{
	book s[5];
int k=s[0].price;
for(int i=1;i<5;i++)
if(s[i].price<k)
k=s[i].price;
 cout<<k;
}

void main()
{
	
modify();

}
Try changing the prototype at line 8
friend void modify();

so that it agrees with the function definition at line 17
void modify(book s[])
Thanks alot Chervil
Topic archived. No new replies allowed.