Click here to Skip to main content
15,793,452 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
Hi, i have created a 2D array from a struct and below is my code.

class RangeValues

     struct RangeStruct{

        int LabelNo;
        double MinValue;
        double MaxValue;
RangeStruct MyRangeArray[2][88];

i have initialized values in RangeValues.cpp class and it works fine...

Now i want to access MyRangeArray from a different class..

RangeValues range;
double LabelValue = range.MyRangeArray[0][1].LabelNo;

But when i print the value it gives something like a big negative number.

LabelValue  -858993460.00000000 

But it shoul equals to 1..(MyRangeArray[0][1].LabelNo contains number 1)

What is wrong with my code? Thank you..
max_nowak 19-Aug-13 9:47am    
please post your initialization code
Dilan Shaminda 19-Aug-13 9:54am    
void RangeValues::LoadRangeValues(const char *range_file_name)
ifstream RangeFile(range_file_name);
string line = " ";
string Token;
int number_of_lines = 0;
getline(RangeFile,line); // avoid the first line
getline(RangeFile,line); // avoid the second line

stringstream ss(line); // Insert the string into a stream

vector<string> tokens;
while (ss >> line)

char *token_string_0 = new char[tokens[0].length() + 1];
strcpy(token_string_0, tokens[0].c_str());
MyRangeArray[MainIndexValue][count].LabelNo = atoi(token_string_0);
char *token_string_1 = new char[tokens[1].length() + 1];
strcpy(token_string_1, tokens[1].c_str());
MyRangeArray[MainIndexValue][count].MinValue = atof(token_string_1);
char *token_string_2 = new char[tokens[2].length() + 1];
strcpy(token_string_2, tokens[2].c_str());
MyRangeArray[MainIndexValue][count].MaxValue = atof(token_string_2);

count = 0;
max_nowak 19-Aug-13 10:01am    
Are you sure this method is called from somewhere within the constructor of your RangeValues object? Because the class above does not even feature a method called 'LoadRangeValues'
Dilan Shaminda 19-Aug-13 10:05am    
yes, there is a method called LoadRangeValues in the class.i just put struct and my array part only.i have printed the values of MyRangeArray values inside RangeValues class and it prints values correctly..Problem is when i access the MyRangeArray from a different class
Stefan_Lang 19-Aug-13 11:13am    
Ok, if from inside the values are ok, then maybe you haven't passed your RangeValues object to the other class correctly? Are you sure the object you are trying to read from is the one you initialized? If you use a pointer to pass the object, is that pointer correct from inside the other class?

1 solution

You can use your code pretty much as is. You are dealing with uninitialized data on the stack. You should add a constructor for your struct, and initialize all values in the struct to something. When you instantiate your class on the stack, the array will have consistent values.
Share this answer

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900