Structure within classes

I created a class, Ballistics, and inside this class I created a series of setters and getters and main function. There is a two dimensional array which I declare. I declare it in the private section at the top of the ballistics class but I don't know its dimensions yet (or at least one of the dimensions). That calculation is done down in the main function where I create a dynamic array. Bear with me, I know this structure is very screwy. Of course, C++ will not tolerate empty dimension. In C, I believe that all but the first dimension must be specified. Here is a short example of what I have:

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

#define START_RANGE 0

private:
float   E[][TOTAL_COLUMNS];   //BAD.  X dimension not specified.
unsigned int MaxRange;
unsigned int RangeInc;
unsigned int nrows;

public:

 void setE(float E[][TOTAL_COLUMNS]) {					
	this->E[nrows+1][TOTAL_COLUMNS] = E[nrows+1][TOTAL_COLUMNS];
                 }

 float getE() {
         return E[nrows+1][TOTAL_COLUMNS];
                 }

void CalculateBallistics(){


 nrows = (unsigned int)ceil( ((float)(Ballistics::getMaxRange() - START_RANGE ) /(float)Ballistics::getRangeInc() ) ); 
					
                   
        //#initialize output array
	//IN C++, must resort to Dynamic Array when dimension unknown.
	float **E = 0;
	E = new float *[nrows + 1];
	for (i = 0; i < nrows + 1; i++)
	E[i] = new float [TOTAL_COLUMNS];



        for (i = 0; i < nrows + 1; i++){
            for (j = 0; j < TOTAL_COLUMNS; j++)
                 E[i][j] = 0;
            }//end for($i



    }//End CalculateBallistics

}//End class Ballistics 


Did I mention to you that this structure is all messed up :o).
What I believe should happen is that E should be globally declared within the class and globally dimensioned (rather than inside the CalculateBallistics function) so that it does not need to be declared again within the CalculateBallistics routine, though I could initialize it to zero in that routine. However, that would require that I run operations (specifically calculation of nrows) to determine its dimensions. This needs to be done within a function in any case.

The structure of the getters and setters are a bit odd to me as well. This was translated from a PHP file I wrote and which does work. PHP appears to be more forgiving. In any case, could someone suggest a better structure for handling this 2D array. Your help would be greatly appreciated.

You can use a vector or a dynamic array.

You can declare a 2D vector without dimensions and then set them in the constructor.

You can also declare 2 int pointers and allocate the dimensions in the constructor.


Something like this.

1
2
3
4
5
6
7
8
9
vector<vector<int> > grid;

Constructor (int GRID_MAX) {
 
	grid.resize(GRID_MAX);

	for (int i = 0; i < GRID_MAX; i++)
	    grid[i].resize(GRID_MAX);
}



If you want dynamic x and y then just use 2 different variables.
Last edited on
multidimensional arrays http://www.cplusplus.com/forum/articles/17108/

However, your problem is more conceptual.
float **E = 0; is a local variable, it will die when `CalculateBallistics()' ends (the array would still be in memory, inaccessible)
review http://cplusplus.com/doc/tutorial/classes/

1
2
3
void setE(float E[][TOTAL_COLUMNS]) {					
	this->E[nrows+1][TOTAL_COLUMNS] = E[nrows+1][TOTAL_COLUMNS];
}
if you were trying to copy the whole array, that's wrong. That's trying to access an element (an invalid element, as you are out of bounds)
Same for float getE() (moreover you can see that you are returning just 1 floating point number)
Topic archived. No new replies allowed.