I was working on a class which will contain an array of objects of another class. But I'm not able to do so. Here is a simplified version of the code I'm trying to use. Can anyone help me work with it?
Why s
cannot access its own data members?
Can anyone show the whole concept of how to deal with these kind of scenarios?
P.S - ONLY USE of FRIEND (class or function) keyword. Inheritance isn't allowed.
class Student{
private:
int roll;
char name1[15];
};
class Subject {
private:
Student s[10];
};
int main()
{
Subject s1;
s1.s[1].roll = 5; // trying to set roll of 1st student
return 0;
}
I was expecting that value will be assigned, but it has the following error. Why it is so?
error: 'Student Subject::s [10]' is private` error: 'int Student::roll' is private error: 'Student Subject::s [10]' is private error: 'int Student::roll' is private