multi-D arrays as instance variables

Hello there, this is Ricardo, I'm also new to this forum. I have been studying C++ for several months now, and lately started learning about arrays in detail. I wrote a small program to practice multi-D arrays but with a slight twist from the book ("Program Development and Design Using C++", Gary Bronson), the arrays are (trying to be!) instance variables for class objects, here is the code:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
class Array
	{
	private:
	string name;
	int ar[5][5][5];
	public:
	Array(string, int[5][5][5]);
	};

Array::Array(string fl1, int fl2[5][5][5])
	{
	name=fl1;
	ar[5][5][5]=fl2[5][5][5];
	cout<<"constructor fl1:\n"<<" name- "<<name<<endl;
	}

int main()
	{
	Array aobj1("first Array-class object", {{1,2,3,4,5},{6,7,8,9,10},{11,12,13,14,15}});
	return 0;
	}


When I compile it (on Ubuntu Linux) I get the following message:

In function 'int main()':
24: error: expected primary-expression before '{' token

I guess I just need to know the proper format to declare a class object with a multi-D array as one of its instance variables.
Thanks in advance
The problem is you aren't initializing the constructor correctly.

1
2
int test[5][5][5] = {{1,2,3,4,5},{6,7,8,9,10},{11,12,13,14,15}};
Array aobj1("first Array-class object", test);


Also, this is totally wrong:

ar[5][5][5]=fl2[5][5][5];

That assigns a single element of the array, not the entire array. (Also, note that the element you're accessing is out of bounds on every dimension). This causes heap corruption. Bad bad bad.

You can't assign full arrays in a single assignment like that. You need to copy them via a for loop:

1
2
3
4
5
for(int x = 0; x < 5; ++x){
  for(int y = 0; y < 5; ++y){
    for(int z = 0; z < 5; ++z){
      ar[x][y][z] = fl2[x][y][z];
}}}


OR, since these are straight arrays and not pointers-to-pointers, you can shortcut:

1
2
for(int x = 0; x < 5*5*5; ++x)
  ar[0][0][x] = fl2[0][0][x];


OR, you can let std::copy do the work:

 
std::copy(&fl2[0][0][0],&fl2[4][4][5],&ar[0][0][0]);



Note how ugly the syntax for all of these are, and how easy it is to screw any of them up. This is because multidimentional arrays are evil. Obligatory link:

http://cplusplus.com/forum/articles/17108/
Thanks for the replies, I can see that posting on this site can be an incredible learning experience! the last two examples are still beyond my knowledge range but I'll look into them more carefully. Actually, the line ar[0][0][x] = fl2[0][0][x];, wouldn't that create a one dimensional array???
Actually, the line ar[0][0][x] = fl2[0][0][x];, wouldn't that create a one dimensional array???


"Straight" Multidimentional arrays basically are 1D arrays... just with fuglified syntax.

For example if you have the following:

1
2
3
4
5
6
int example[2][3];

example[a][b] = 0;  // if you try to do this
 // the compiler basically translates this to:

example[0][ (a*3) + b ] = 0;


This is because the array data is stored contiguously in memory:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
int example[2][3] = { {0,1,2},{3,4,5} };

// 'example' is conceptually stored as:
0 1 2
3 4 5

// but that's basically the same as this:
0 1 2 3 4 5
// because memory is linear

// so if you do this:
cout << example[1][2];  // this will print '4' for obvious reasons

// but if you try this:
cout << example[0][4];  // this will also print '4' because it's layed out
    // in memory like a normal 1D array 



Note that this approach WILL NOT WORK if you have "nested new" or "pointer to pointer" style MD arrays, as those are totally different under the hood.
Wow, my book doesn't explain this so clearly!
So in conclusion, there are two ways to access straight multidimensional arrays, conceptually ( example[1][2] ) and linearly ( example[0][4] ), right?
Topic archived. No new replies allowed.