classes c++

this is my header file.. sorted_list.h
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
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
1
2
3
4
sorted_list a;

a.insert(j,i);


in the implementation file sorted_list.cc

1
2
3
4
5
6
7
8
9
10
11
12
13
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...??
how can i access the private variables of an inner class...??


reading threw your posted sorce, the way i could see it done is have sorted_list have a public "get" method, and in that method have it call a "get" public method of list_link, that returns whatever values you want

main calls sorted_list_get_list_link()
sorted_list_get_list_link() calls list_link_get_stuff()
list_link_get_stuff() returns values you want

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
class sorted_list
{

private:
class list_link
{
private:
int key; 
double value; 
class list_link* next; 
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:
1
2
3
4
sorted_list a;

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


implementation file sorted_list.cc
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15

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)

}


Just make the variable public (the class is private so you can't access it from outside).
Or handle better the insert method (why you want to modify key?)
Topic archived. No new replies allowed.