使成员成为嵌套类 [英] making a member fo a nested class
本文介绍了使成员成为嵌套类的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
#include"StdAfx.h"
#include<iostream>
#include<cstddef>
#include<ctime>
//#include<WinBase.h
using namespace std;
class college //outer class
{
class student //inner class 1
{
int year,rollno;
char *name,*stream;
student *next,*prev;
};
class accounts //
{
time_t now;
tm *ltm;
int current_year;
int current_month;
int current_date;
int current_hour;
int current_minute;
int current_sec;
int semester_fees;
int breakpoints[7][7];
int amount;
};
public:
class linklist
{
student *start;
student *temp;
student *next;
student *prev;
public:
linklist(char *name,char *stream,int year,int rollno)
{
student *ptr=new student();
start=ptr;
ptr->name=name;
ptr->rollno=rollno;
ptr->stream=stream;
ptr->year=year;
temp=start;
}
void append(char *name,char *stream,int year,int rollno)
{
student *ptr1=new student();
ptr1->name=name; //why? error
ptr1->stream=stream; //why error?
ptr1->year=year; //why error?
ptr1->rollno=rollno; //why error?
}
char* current_name()
{
return temp->name;
}
char* current_stream()
{
return temp->stream;
}
int current_rollno()
{
return temp->rollno;
}
int current_year()
{
return temp->year;
}
};
};
int main()
{
college::linklist l("bhawin","ecn",1991,123);
return 0;
}
当我尝试访问学生班级成员时出错,为什么?
there is error when i try to access the member of student class, why?
推荐答案
您无法访问学生的私人成员
类(默认类成员的可见性私有
)。这是一个通用的C ++
规则(与嵌套类无关)。
你要么要
- 为
student
类成员(即student
类本身中的get / set方法)提供访问器。- 更改为
public
学生班级成员的可见性。- 声明
朋友
需要访问学生
班级成员的班级。
You cannot access private members of thestudent
class (default visibility for class members isprivate
). It is a generalC++
rule (has nothing to do with nested classes).
You have either to
- Provide accessors for
student
class members (that is get/set methods instudent
class itself).- Change to
public
the visibility of student class members.- Declare
friend
the classes that need to accessstudent
class members.
这篇关于使成员成为嵌套类的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文