C++ 在属性私有化方面有困难

C++ 在属性私有化方面有困难,c++,class,private,C++,Class,Private,这是我收到的编译器错误 project1.cc:In functionstd::vector ReturnCourseenRolliment(int)': project1.cc:84:错误:课程注册::课程“是私有的 project1.cc:293:错误:在此上下文中 我的代码: 290 vector<Enrollment> e; 291 for (int i=0; i<Enrollment::enrollmentList.size();i++) { 292

这是我收到的编译器错误

project1.cc:In function
std::vector
ReturnCourseenRolliment(int)':
project1.cc:84:错误:
课程注册::课程“是私有的

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     }
290向量e;
291表示(inti=0;i>id))id=0;
234国际空间站忽略(1,,);
235如果(!(iss>>id2))id2=0;

236不能怀疑你有以下情况:

class Enrollment
{
    // ...
    Course course;
};
您应确保
课程
成员出现在
公共
课程部分,以便按如下方式访问:

class Enrollment
{
public:
    Course course;
};
另一个选项是将
returnCourseenRolliment()
函数声明为
注册的
朋友

class Enrollment;

class Enrollment
{
private:
    friend std::vector<Enrollment, std::allocator<Enrollment> > 
               returnCourseEnrollment(int);
    Course course;
};
班级注册;
班级注册
{
私人:
friend std::vector
返回课程登记(int);
课程;
};

我如何才能访问我正在创建的方法中的私有属性?
您将该函数声明为一个
朋友
,或者将该数据成员
公开
。或者您可以引入一个公共访问器方法,并使用它。不幸的是,我现在有一个分段错误。在注释掉行之后,我已经确定了导致问题的行Enrollment::enrollmentList[I]=newEnrollment;对不起,这是另一个问题,是由不同的事情引起的!试着调试(一步一步地调试代码),以找到seg故障实际发生的位置。我在您之前评论了seg故障的来源。
class Enrollment
{
public:
    Course course;
};
class Enrollment;

class Enrollment
{
private:
    friend std::vector<Enrollment, std::allocator<Enrollment> > 
               returnCourseEnrollment(int);
    Course course;
};