why cant i modify the value of a (private) member variable

i can not change the value of "int index" no matter what i try
i have tried thus far using
1)index++
2)this->index++
3)using a set_index() fucntion
4)changing "index" to "*index" and changing the value it points to
5)buffered_reader::index = index+1;

note: the value of "index" does change during the scope of the get_next_data() but the func if over it returns to it's original value

shortened code(contains only the things i think are relevant to the question)
1
2
3
4
5
6
7
8
9
class buffered_reader {
	private:
		int index;
	public:
		buffered_reader(){};
                //this does not change the value of index 
		int get_next_data(){index++;}
};

full code (in-case i missed something important)
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
#ifndef read_buf
#define read_buf
#include <iostream>
#include <string>
#include <fstream>
#include "definitions.h"
#define FILE_SIZE 10

class buffered_reader {
	private:
		//DATA_T buffer[buffer_size];
		DATA_T* buffer = (DATA_T*) malloc(sizeof(DATA_T)*buffer_size);
		std::ifstream *source_file;
		int index;
		int buffer_counter;
	public:
		buffered_reader(){};
		buffered_reader(std::string);
		DATA_T get_data() {return buffer[index];};
		//Returns 0 when there is not new data
		int get_next_data(){
				//this is for debuging
				std::cout<<"currentSize:"<<index<<std::endl;
				index++;
			if (index == buffer_size){
				assert(0);
				if (++buffer_counter == FILE_SIZE){
					source_file->close();
					return 0;
				}
				source_file->read((char*) buffer, page_size);
				index = 0;
			}
			return 1;
		}
};

buffered_reader::buffered_reader(std::string path){
	source_file = new std::ifstream();
	source_file->open(path, std::ios::binary);
	if(!source_file->is_open()){
		std::cout << "ERRNO: Could not open file:" << path << '\n';
		exit(EXIT_FAILURE);
	}

	source_file->read((char*) buffer, page_size);
	index = 0;
	buffer_counter = 0;
}

#endif 


thanks for any help
Last edited on
The error doesn't seem to be in the code you've posted.
Maybe your buffered_reader object is constantly going into and out of scope in your program?

Note that you should have a destructor to delete source_file (which will also close the file).
And it looks like get_next_data should return a bool.
And you shouldn't use malloc!
Last edited on
assuming the question is tied to an error message... you must be doing this sort of thing somewhere:

class oops
{
int i;
};

main()
{
oops o;
o.i = 3; //error, cannot access private member.
}

in your main, or another chunk of code, did you try to read or modify index??
@dutch
thanks for tips

@jonnin
the issue was from outside the class
i was coping the class instead of getting a pointer to it in my data structure
Topic archived. No new replies allowed.