Accessing members of another class

Hello. I am trying to write a function (setxy in class A) in one class to access members (function sety in class B) of another class. I am not sure what is wrong with my code. Please help.

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
  #include <iostream>
using namespace std;

class A {
 public:
  int x;
 public:
  A() { x = 0;}
  void setxy(B & obj)
  {
	 obj.sety();
   }
};

class B {
public:
	int y;
	void sety(){
		y=0;
		cout<<y<<endl;}};

	int main(){
	A obja;
	B objb;
	obja.setxy(objb);
	return 0;
		}
The issue is that, when compiling class A, you reference class B's function... except class B hasn't been compiled yet.

Try moving class B above class A in the file.
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
#include <iostream>
using namespace std;

class B 
{
public:
	int y;
	void sety()
	{
		y=0;
		cout << y << endl;
	}
};


class A 
{
 private: //
  int x;
 public:
  A() { x = 0;}
  void setxy(B & obj)
  {
	 obj.sety();
   }
};


int main(){
	A obja;
	B objb;
	obja.setxy(objb);
	return 0;
}
Thanks a lot. It works now. I've spent hours on this stupid mistake.
Topic archived. No new replies allowed.