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>
usingnamespace 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:
friendclass 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>
usingnamespace std;
class A {
int b;
private:
friendvoid Set(int);
};
void Set(int c) {
b = c;
}
"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.