C ++ Inserting 2D Object array into another 2D Object array
When using Dev C ++ I am trying to insert a smaller 2D array object into a larger 2D array object. While trying to achieve this, I ran into compiler errors that I don't know how to solve.
I am trying to insert a smaller object by forcing it to return the name of the array. Then I try to change the values ββinside the large array with the values ββof the smaller array.
There are two lines of code that I'm having problems with:
int result = smallerArray.extractPiece();
largerArray.extractArray(result);
And in these two lines of codes:
int Piece::extractPiece()
{
return **pieceArray;
}
and
void Grid :: extractArray (int ** arr) { for (int i = 0; i & lt xGrid; ++ i) { for (int j = 0; j & lt yGrid; ++ j) { squares [i] [j] = arr [i] [j]; } } }
Two problems: "int result" will not contain lessArray.extractPiece (), and if I just put "lessArray.extractPiece ()" in largeArray.extractArray (), I still have problems. I tried to make "int result" a pointer to a pointer like "int ** result", I still have the same errors.
These are the errors I get when I try to compile in Dev C ++:
In function `int main()';
invalid conversion from `int' to `int**'
initlizing argument 1 of 'void Grid::extractArray(int**)'
[Build Error] [grid test.o] Error 1
Does anyone know what is wrong?
It is this chain of code:
int result = smallerArray.extractPiece();
largerArray.extractArray(result);
// ...
int Piece::extractPiece() {
return **pieceArray;
}
Trying to pass an int to extractArray which wants a pointer to a pointer, presumably your dynamic array, not an int. Try changing it to
int **result = smallerArray.extractPiece();
largerArray.extractArray(result);
// ...
int ** Piece::extractPiece() {
return pieceArray;
}
Only changing the result to pointer to pointer will not work. You should of course also change what extractPiece returns (change from int to int **)
source to share
Look, it was always, at least for me, easier to manage two-dimensional arrays internally as 1D arrays, where M [i, j] = A [i * N + j], where N is the number of cols (or rows if 2D- arrays are the type of row columns). Users can get items with indices i, j, but my class always stores A [M * ββN] as private data. Passing 1-D-pointer arrays is easier than manipulating 2-D-pointer arrays (you can't get caught up in pointer-to-pointer syntax, which can get messy in some code).
It is not related to this question, but since I am not aware of certain instrinsics of compiler optimizations, I am wondering if M [i, j] gets converted to [i] internally to use simpler addressing modes in the generated code.
source to share