friend Class Function to read other Class private int

I'm trying to let one particular function of class B to read a private variable "number" in a class A. Compilation of code below gives me plenty of errors, what I did wrong, what should I change?

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

class A
{
public:
	A(int nr){number=nr;}
private:
	int number;
	friend void B.Ou(int number);
};

class B
{
public:
	void Ou(int number)
	{
		cout<<number<<endl;
	}
};

void main()
{
	A a_object(10);
	B b_object;
	b_object.Ou(a_object.number);
}
This is a little tricky, as each class A and B depends on the other. First, the friend function should be declared as public, and use the scope resolution B:: instead of B.

This is the code which I have tried:
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
#include <iostream>
#include <conio.h>

using namespace std;

class A;

class B
{
public:
    void Ou(A & rhs);
};

class A
{
public:
    A(int nr) {number = nr;}
    friend void B::Ou(A & rhs);
private:
    int number;

};

void B::Ou(A & rhs)
{
    cout << rhs.number << endl;
}

int  main()
{
    A a_object(10);
    B b_object;
    b_object.Ou(a_object);

    getch();
    return 0;
}
Works like charm! Thanks!
Topic archived. No new replies allowed.