Friend Functions

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
#include <iostream>
using namespace std;
const int IDLE=0;
const int INUSE=1;
class C2; // forward declaration
class C1 {
int status; // IDLE if off, INUSE if on screen
// ...
public:
void set_status(int state);
int idle(C2 b); // now a member of C1
};
class C2 {
int status; // IDLE if off, INUSE if on screen
// ...
public:
void set_status(int state);
friend int C1::idle(C2 b); // a friend, here
};
void C1::set_status(int state)
{
status = state;
}
void C2::set_status(int state)
{
status = state;
}
// idle() is member of C1, but friend of C2.
int C1::idle(C2 b)
{
if(status || b.status) return 0; // why this line shows error ?
else return 1;
}


int main()
{
C1 x;
C2 y;
x.set_status(IDLE);
y.set_status(IDLE);
if(x.idle(y)) cout << "Screen Can Be Used.\n";
else cout << "Pop-up In Use.\n";
x.set_status(INUSE);
if(x.idle(y)) cout << "Screen Can Be Used.\n";
else cout << "Pop-up In Use.\n";
getchar();
return 0;
}

if(status || b.status) return 0; // why this line shows error ?
I copied your program as it is, and it is compiling for me.
I am using visual studio 2012 .... I compiles but show red line under b.status
Sometimes intellisense gets confused. Try cleaning your solution, restarting VS, then rebuilding everything.
k. thanks
i'm working on something that has 37 projects in the VS solution. Intellisense gets very confused :)
Basically, if there's red lines but it compiles, ignore the red lines.
Topic archived. No new replies allowed.