problem with friend and inheritance

i have been learning friendship and inheritance and i am stuck in front of the same problem for 5 hours and cant find any answer why doesn't it work:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
#include<iostream>
using namespace std;

class Printer {
public:
	void Set_Values(int, int);
};

void Printer::Set_Values(int a, int b) {
	Width = a;
	Height = b;
}

class Shape {
private:
	int Width, Height;
public:
	friend class Printer;
};


by the sites guide the friend class Shape should give Printer access to width and Height yet i get an error saying they are not members what am i doing wrong?

Sites quote:"Just as we have the possibility to define a friend function, we can also define a class as friend of another one,
granting that first class access to the protected and private members of the second one"

the same error applies to friend functions:

1
2
3
4
5
6
7
8
9
10
11
12
#include<iostream>
using namespace std;

class A {
	int b;
private:
	friend void Set(int);
};

void Set(int c) {
	b = c;
}
Last edited on
"Friend" gives you access to the private members of a class instance. What you're missing here is that you haven't specified which "Shape" instance you want to set. To see this, temporarily change Width and Height to be public and you'll see that the problem is still present.
i still don't understand where my mistake is can you please give an example ?
closed account (48T7M4Gy)
https://en.m.wikipedia.org/wiki/Friend_class
Thanks now i understand how it works!
Topic archived. No new replies allowed.