Click here to Skip to main content
15,892,199 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
C++
#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?
Posted
Updated 16-Jul-13 21:28pm
v2

1 solution

You cannot access private members of the student class (default visibility for class members is private). It is a general C++ rule (has nothing to do with nested classes).
You have either to
  • Provide accessors for student class members (that is get/set methods in student class itself).
  • Change to public the visibility of student class members.
  • Declare friend the classes that need to access student class members.
 
Share this answer
 
Comments
[no name] 17-Jul-13 4:04am    
Yes the classic "what is the difference between a class and a structure" question.
CPallini 17-Jul-13 15:05pm    
Kind of that, thank you.
H.Brydon 17-Jul-13 12:56pm    
Sounds right. Further success might also be achieved by putting a "public:" before the student class definition (and inside accounts, and as you say, as well as inside the student class).

And a +5 for CPallini
CPallini 17-Jul-13 15:05pm    
Thank you.
bhawin parkeria 17-Jul-13 14:02pm    
my bad

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900