How to access members of a Class?


I have a program that looks likes this. And a class with a method "Read_text" that will populate the array "s_text[][]" and also enter the contents into "Full_text".

Somehow, I just can't seems to access the elements in obj_A.s_Text[][]. I am not sure what is wrong with "SomeText=obj_A.s_Text[2][2]". I have tested during breakpoints to monitor and confirm that the array is properly popluated with correct data. However, when I try to access from the main() function, can't seems to access the data at all.

Sorry if the post is difficult to read as I'm just a beginner.

Thanks for any advises.

Part of the code is produced below.

=====================

void main()
{
CSomeClass obj_A;
CString SomeText;

obj_A.Read_Text(); // Read Data

MessageBox (obj_A.Full_Text) // Shows the contents

SomeText=obj_A.s_Text[2][2] // Unable to read the contents

}



class CSomeClass
{
public:
void Read_Text();
CString Full_Text;
CString s_Text[5][5];

};
Hmmm. From the code posted, things look okay to me. Could you post the implementation of CSomeClass::Read_Text and perhaps that will help.

I took the liberty of making one myself, and things seem to work okay. I converted this to use the console, plus "standard" c++ rather than Microsoft specific, just to make it easy for me.

I hope this helps

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
#include <string>
#include <iostream>

class CSomeClass
{
public:
	void Read_Text();
	std::string Full_Text;
	std::string s_Text[5][5];
};

void CSomeClass::Read_Text() {
	for(int i = 0; i < 5; i++) {
		for (int j = 0; j < 5; j++) {
			s_Text[i][j] = "Hello";
			Full_Text += s_Text[i][j];
		}
	}
}

void main()
{
	CSomeClass obj_A;
	std::string SomeText;

	obj_A.Read_Text(); // Read Data

	std::cout << obj_A.Full_Text << std::endl; // Shows the contents

	SomeText=obj_A.s_Text[2][2]; // assigns s_Text[2][2] to SomeText
}
Last edited on
Topic archived. No new replies allowed.