Вот ошибка компилятора, которую я получил.
project1.cc: В функции std::vector<Enrollment, std::allocator<Enrollment> > returnCourseEnrollment(int)': project1.cc:84: error:
Регистрация курса :: курс' частный
project1.cc:293: ошибка: в этом контексте
мой код:
290 vector<Enrollment> e;
291 for (int i=0; i<Enrollment::enrollmentList.size();i++) {
292 Enrollment enroll = Enrollment::enrollmentList[i];
293 if (enroll.course.getCourseID()) e.push_back(enroll);
294 }
Я просматриваю вектор, чтобы соответствовать идентификатору курса со студентами курса.
231 string myText(line);
232 istringstream iss(myText);
233 if (!(iss>>id)) id = 0;
234 iss.ignore(1,',');
235 if (!(iss>>id2)) id2 = 0;
236 cout<<"id: "<<id<<" id2: "<<id2<<endl;
237 Enrollment newEnrollment(Student::studentList[id],Course::courseList[id2]);
238 Enrollment::enrollmentList[i] = newEnrollment;
239 i++;
Здесь выполняется вектор. Как вы можете видеть, каждый элемент вектора занимает экземпляр класса. Любые рекомендации о том, как я могу получить доступ к частным атрибутам в методе, который я делаю?
edit: вот класс регистрации
82 private:
83 Student student;
84 Course course;
85
86 public:
87 Enrollment(Student student,Course course):student(student),course(course){}
88 Enrollment(){}
89 static vector<Enrollment> enrollmentList;
90 static int loadEnrollment();
91 static vector<Enrollment> returnCourseEnrollment(int courseid);
92 static vector<Enrollment> returnSchedule(int studentid);
93 friend ostream& operator<< (ostream& out,Enrollment e) {
94 out<<e.student.getName()<<"("<<e.student.getId()<<")"<<e.course.getName()<<"("<<e.course.getId()<<")";
95 return out;
Подозреваю, что у вас есть что-то вроде:
class Enrollment
{
// ...
Course course;
};
вы должны убедиться, что член course
появится в разделе public
класса, чтобы он был доступен следующим образом:
class Enrollment
{
public:
Course course;
};
Другой вариант - объявить функцию returnCourseEnrollment()
чтобы быть friend
Enrollment
:
class Enrollment;
class Enrollment
{
private:
friend std::vector<Enrollment, std::allocator<Enrollment> >
returnCourseEnrollment(int);
Course course;
};
how I can gain access to the private attributes in the method I am making?
Вы объявляете эту функциюfriend
или делаете этот элемент данныхpublic
. Или, возможно, вы могли бы ввести публичный метод доступа и использовать его.