属性是私有的

时间:2014-02-23 21:01:30

标签: c++ class private

这是我收到的编译错误。

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     }

我正在搜索一个向量,以便将课程ID与课程中的学生进行匹配。

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++;

这是制作矢量的地方。正如您所看到的,矢量的每个元素都被实例占用了一个类。关于如何在我正在制作的方法中访问私有属性的任何建议?

编辑:这是注册类

 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;

1 个答案:

答案 0 :(得分:0)

怀疑你有类似的东西:

class Enrollment
{
    // ...
    Course course;
};

您应该确保course类成员出现在public类部分中,以便按以下方式访问:

class Enrollment
{
public:
    Course course;
};

另一种选择是将returnCourseEnrollment()函数声明为friend的{​​{1}}:

Enrollment