Я пытаюсь сделать статический член в своем классе, но компилятор выдает мне ошибку, что SubjectList :: X не определен. То, что я пытаюсь сделать, это то, что я сделаю список из 2 предметов. А потом я попрошу студента выбрать один из этих двух предметов. Так что я хочу сделать их свойство класса вместо свойства объекта. Потому что, если я не сделаю его статичным, каждый студент попросит указать название предмета и собственный код.
#include <iostream>
#include <string.h>
using namespace std;
class Subject{
public:
friend class SubjectList;
//Subject() {}
Subject(){
cout << "Enter Subject name - ";
cin >> name;
cout << "Enter subject code - ";
cin >> code;
}
private:
int code;
string name;
};
class SubjectValue{
friend class SubjectList;
int value;
};
class SubjectList{
public:
friend class Student;
static Subject X[2]; // HERE IT GIVES ERROR.
SubjectValue sv[2];
set_subject();
show_subject();
};
class Student{
public:
friend class StudentList;
SubjectList sub;
private:
unsigned long int roll;
unsigned long long int phone;
string name;
};
class StudentList{
public:
Student s[2];
set_data();
show_data();
};
StudentList::set_data()
{
int i ;
for(i = 0; i < 2; i++){
cout << "Enter student name - ";
cin >> s[i].name;
cout << "Enter student phone - ";
cin >> s[i].phone;
cout << "Enter roll - ";
cin >> s[i].roll;
s[i].sub.set_subject();
}
}
StudentList::show_data()
{
int i;
for(i = 0; i < 2; i++){
cout << "Name of Student - " << s[i].name << endl;
cout << "Phone of Student - " << s[i].phone << endl;
cout << "Roll of Student - " << s[i].roll << endl;
s[i].sub.show_subject();
}
}
SubjectList::set_subject()
{
int i;
int value[2];
cout << "Enter 1 for the subject you want to choose. \n";
//cout << "Subject " << (i+1);
for(i = 0; i < 2; i++)
{
cin >> value[i];
}
for(i = 0; i < 2; i++)
{
sv[i].value = value[i];
}
}
SubjectList::show_subject()
{
int i;
cout << "Subjects choose by student are - " << endl;
for(i = 0; i < 2; i++)
{
if (sv[i].value == 1)
{
cout << X[i].name << endl;
}
}
}
int main()
{
StudentList s;
s.set_data();
s.show_data();
return 0;
}