Calling an object within a class

1
2
3
4
5
6
7
8
class abc {

	public:

	int i;
	abc * foooo;

};


How do you call * foooo? Say I create:

abc a;

* foooo would have some values for int i.

To get int i of *foooo, I tried a.foooo.i, which doesn't work. How do you call it?
Last edited on
To access a class member through a pointer, you need to use the -> operator.
1
2
3
4
5
abc a, b;

a.foooo = &b;

a.foooo->i = 77; //Same as writing b.i = 77; 
foooo is a pointer, so a.foooo->i
make sure that it does point to a valid location.
In the code below(I omitted the stuff that I don't think will be important) row_str and col_str are strings input by the user.
a[i][j] is a two dimensional dynamic array of type 'abc'.

Here is a more descriptive class:

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
class abc {

	public:

	int i;
	int j;
	abc * back;

};

int d(string &s, string &t, int i, int j)
{
	if(s[i]==t[j])
	{
		return 0;

	}

	else  

		return 1;

}

int min(int a, int b, int c)

{
	int x[3]={a,b,c};
	int y=a;

	for(int i=1; i<3; i++)
	{
		if(x[i]<y)

			y=x[i];

	}

	return y;

}


In the main function, I am getting segmentation fault error on anything that uses arrows '->'. My teacher hasn't taught how to use arrows yet, so I don't really know how to use it properly.


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

for(int r=1; r<row_str.length(); r++)

	{



		for(int c=1; c<col_str.length(); c++)

		{

			a[r][c].value=min(a[r-1][c-1].value+d(row_str,col_str,r,c), a[r-1][c].value+1, a[r][c-1].value+1);

			if(a[r][c].value==a[r-1][c-1].value+d(row_str,col_str,r,c))
			{
//segmentation fault errors in lines with arrows.
//Basically I want a[r][c].back->i to equal the last row r-1.
//And I want a[r][c].back->j to equal the last column c-1.
				a[r][c].back->i= r-1;
				a[r][c].back->j= c-1;

			}

			else if(a[r][c].value==a[r-1][c].value+1)
			{
				a[r][c].back->i= r-1;
				a[r][c].back->j= c;

			}
			else if(a[r][c].value==a[r][c-1].value+1)

			{
				a[r][c].back->i= r;
				a[r][c].back->j= c-1;

			}

		}


	}



Full code is below:

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
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
#include <iostream>
#include <string>
using namespace std;

class Entry {

	public:

	int i;
	int j;
	int value;
	//Entry *back can be set as an array.
	Entry * back;

};

int d(string &s, string &t, int i, int j)
{
	if(s[i]==t[j])
	{

		return 0;

	}

	else  

		return 1;

}



int min(int a, int b, int c)
{


	int x[3]={a,b,c};

	int y=a;

	for(int i=1; i<3; i++)
	{

		if(x[i]<y)

			y=x[i];

	}

	return y;

}



int main()
{

	string row_str="_", col_str="_";

	string row_in, col_in;



 	cout<<"This program outputs the edit distance of two strings.\n"

			<<"Enter the first string.\n";

	cin>>row_in;

	cout<<"Enter the second string.\n";

	cin>>col_in;

	row_str=row_str+row_in;

	col_str=col_str+col_in;

	Entry**a=new Entry*[row_str.length()];

	for(int i=0; i<row_str.length(); i++)
	{

		a[i]=new Entry[col_str.length()];

	}


	for(int x=0; x<row_str.length(); x++)
	{

		a[x][0].value=x;
		a[x][0].i=0;
		a[x][0].j=x;


		//cout<<"a[x][0].i: " << a[x][0].i << "x: " << x << ".\n";

	}


	for(int y=0; y<col_str.length(); y++)

	{

		a[0][y].value=y;
		a[0][y].i=y;
		a[0][y].j=0;

		//cout<<"a[0][y].j: " << a[0][y].j << "y: " << y << ".\n";

	}



/*
	for(int r=0; r<row_str.length(); r++)

	{

		for(int c=0; c<col_str.length(); c++)
		{

				cout<<a[r][c].value<<' ';

		}

		cout<<"\n";

	}
*/

	int e_size=0;
	
	if(	col_str.length()>=row_str.length() )
		e_size=col_str.length();

	else if( col_str.length()<=row_str.length() )
		e_size=row_str.length();

	//a[0][0].back=new Entry[e_size];


	for(int r=1; r<row_str.length(); r++)
	{

		for(int c=1; c<col_str.length(); c++)
		{
			a[r][c].value=min(a[r-1][c-1].value+d(row_str,col_str,r,c), a[r-1][c].value+1, a[r][c-1].value+1);
			cout<< "r: " << r << "c: " << c << "\n";
			cout<<"a[r][c].value: " << a[r][c].value << "\n";
			cout<<"a[r-1][c-1].value+d(row_str,col_str,r,c): " << a[r-1][c-1].value+d(row_str,col_str,r,c) << "\n";	
			cout<<"a[r-1][c].value+1: " << a[r-1][c].value+1 << "\n";	
			cout<<"a[r][c-1].value+1: " << a[r][c-1].value+1 << "\n";	
			cout<<"a[r][c].back: " << a[r][c].back << "\n";	
			cout<<"a[r][c].back->i: " << a[r][c].back->i << "\n";
	

			if(a[r][c].value==a[r-1][c-1].value+d(row_str,col_str,r,c))
			{
			//This assigns to *a[r][c].back the value a[r-1][c-1], because a[r][c] is jumped from a[r-1][c-1].


				a[r][c].back->i= &a[r-1][c-1];
				//a[r][c].back->j= x.j;

				cout<<"a[r][c].back->i: " << a[r][c].back->i << "\n";

			}

			else if(a[r][c].value==a[r-1][c].value+1)
			{
				a[r][c].back->i= r-1;
				a[r][c].back->j= c;

			}
			else if(a[r][c].value==a[r][c-1].value+1)

			{
				a[r][c].back->i= r;
				a[r][c].back->j= c-1;

			}

		}

	}


	cout<<"The rows represent the first string you entered plus a beginning space character: \n"

			<<row_str<<"\n"

		  <<"The columns represent the second string you entered plus a beginning space character: \n"

			<<col_str<<"\n";

	cout<<"\n";

	cout<<"\n\nThe editing distance is:\n"
	  <<a[row_str.length()-1][col_str.length()-1].value;
Last edited on
In the main function, I am getting segmentation fault error on anything that uses arrows '->'. My teacher hasn't taught how to use arrows yet, so I don't really know how to use it properly.


I will do you a big favor. http://lmgtfy.com/?q=c%2B%2B+arrow+operator
Look at it this way - a->b is the same as (*a).b.

Now, in your program, you are dereferencing back. However, you never set back to point to anything, and then you are setting 'i' to the address of something else; but in the process of doing this you dereference back (which isn't pointing anywhere sensible yet). You may want to reconsider lines 148 to 188.
Last edited on
Topic archived. No new replies allowed.