this is my header file.. sorted_list.h
class sorted_list
{

private:
class list_link
{
private:
int key; // identifies the data
double value; // the data stored
class list_link* next; // a pointer to the next data
public:

list_link(int,double,list_link*);
~list_link();
};

class list_link* first;

public:

sorted_list();

~sorted_list();

void insert(int key, double value);

void remove(int key);

void copy();

class my_iterator
{

private:

class list_link* current; // a pointer to the "current" list element

public:

class my_iterator list_begin();

class my_iterator list_end();

bool iterator_end(class my_iterator* i);

class my_iterator iterator_next(class my_iterator* i);

int iterator_get_key(class my_iterator* i);

double iterator_get_value(class my_iterator* i);
};

};

i tried to access the private variables of list_link through list_link(int,double,list_link*); constructor the compiler is giving me an error...

in the main file
sorted_list a;

a.insert(j,i);


in the implementation file sorted_list.cc

sorted_list::list_link::list_link(int key, double value,list_link* next=0)
{
this->key=key;
this->value=value;
this->next=next;
}


void sorted_list::insert(int key, double value)
{
sorted_list::list_link *temp,*current;
temp=new list_link(key,value);
temp->key;(it is giving me an error here saying that list is a private variable)

}

how can i access the private variables of an inner class...??

Please use code tags when posting code.

The way you are accessing it with temp->key; indeed requires key to be a public member. Nested classes don't get any special privileged (i.e. just because you have defined a class inside of a class doesn't mean it gets access to the outside classes private members). You would have to make an accessor like sorted_list::GetKey(){return this->key;} David

class sorted_list
{
private:
class list_link
{
private:
int key; // identifies the data
double value; // the data stored
class list_link* next; // a pointer to the next data
public:

list_link(int,double,list_link*);
~list_link();
};

class list_link* first;

public:

sorted_list();

~sorted_list();

void insert(int key, double value);

void remove(int key);

void copy();
};

hi i think that is what i have done..

the main function calls the public insert function void insert(int key, double value);

main function:

sorted_list a;

a.insert(j,i);calls the public insert function

implementation file sorted_list.cc

sorted_list::list_link::list_link(int key, double value,list_link* next=0)(gets called by the insert function)
{
this->key=key;
this->value=value;
this->next=next;
}


void sorted_list::insert(int key, double value)
{
sorted_list::list_link *temp,*current;
temp=new list_link(key,value); here the public constructor is getting called by the insert function

temp->key;(it is giving me an error here saying that list is a private variable)

hi i think i have done the way you have mentioned to do.. but if this is not what you meant can you please explain me in detail.. or change the code above so that i can have good understanding of what you are saying....

This article has been dead for over six months. Start a new discussion instead.