forward reference error

Hi everyone,

i am in bit of trouble at compiling my code. so, i hope you guys will help me out by pointing out the problem thus i may learn.

Here is my code:
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
69
70
71
72
73
74

#include <iostream>

using namespace std;

const int IDLE = 0;
const int INUSE = 1;

class C2;

class C1{

	int status;
	
	public:
		void set_status(int state);
		//int inuse(C1 a, C2 b);
		int idle(C2 b);
};
	
	void C1 :: set_status(int state){
		status = state;
	}
	
	int C1 :: idle(C2 b){
		if( status || b.status ) return 0;
		else return 1;
	}
	
	/*int C1 :: inuse(C1 a, C2 b){
		if( a.status || b.status ) return 1;
		else return 0;
	}*/
	
class C2{
	
	int status;
	
	public:
		void set_status(int state);
		//friend int C1 :: inuse(C1 a, C2 b);
		friend int C1 :: idle(C2 b);
		
};

	void C2 :: set_status(int state){
		status = state;
	}
// Defining The friend.

/*int idle(C1 a, C2 b){
	if( a.status || b.status ) return 0;
	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 << "In use.\n";
	
	/*x.set_status(INUSE);
	y.set_status(INUSE);
	
	if(x.inuse(x,y)) cout << "Screen can not be used.\n";
	else cout << "Not in use.\n";*/
	
	return 0;
}
	


The problem is when i try to compile the code i get a error message of forward declaration error for class C2. i don't understand why ??

Last edited on
you got 2 class definitions... 1 on line 9 and 1 on line 35.
remove the one on line 9...
but if don't use the first declaration then C1 doesn't sees C2 while defining a member function which uses C2 definition. Thus, i have used the forward declaration but it's showing me error. I have also tried your solution but got more errors. Thanks anyway.
ah i see..

maybe change int idle(C2 b); to int idle(C2* b);
Last edited on
tried but still problem is at line 9 telling forward declaration error.
well, i found the problem. it's in the definition of the member functions. i have to define the class that i have declared with forward declaration before i define the member functions who are using the class type. thus, the problem was solved. Here is the updated code
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
69
70
71
72
73
74
75
76
77
78
79
80
81
82
/*
	Date: 6-11-12
	Author: Sheyam
	Description: Use of Friend Function.
*/


#include <iostream>

using namespace std;

const int IDLE = 0;
const int INUSE = 1;

class C2;

class C1{

	int status;
	
	public:
		void set_status(int state);
		//int inuse(C1 a, C2 b);
		int idle(C2 b);
};

class C2{
	
	int status;
	
	public:
		void set_status(int state);
		//friend int C1 :: inuse(C1 a, C2 b);
		friend int C1 :: idle(C2 b);
		
};
	
	void C1 :: set_status(int state){
		status = state;
	}
	
	int C1 :: idle(C2 b){
		if( status || b.status ) return 0;
		else return 1;
	}
	
	/*int C1 :: inuse(C1 a, C2 b){
		if( a.status || b.status ) return 1;
		else return 0;
	}*/
	

	void C2 :: set_status(int state){
		status = state;
	}
// Defining The friend.

/*int idle(C1 a, C2 b){
	if( a.status || b.status ) return 0;
	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 << "In use.\n";
	
	/*x.set_status(INUSE);
	y.set_status(INUSE);
	
	if(x.inuse(x,y)) cout << "Screen can not be used.\n";
	else cout << "Not in use.\n";*/
	
	return 0;
}

thanks.	
Topic archived. No new replies allowed.