C ++ 2DArray objects; Problems with pointers and arrays
This problem is solved from the posed challenge in my old question, which is from: C ++ Inserting a 2D array object into another 2D array object
But also created a new problem for me. Please read the question and solution in the link to understand my problem. The solution in the previous question was to make the Data Member Function a pointer to a pointer in order to allow passage into another data member function. But to fix this, the first data member function smallerArray.extractPiece()
now only returns the address of a pointer to a pointer, not the contents of those pointers. I need the content in order for my 2nd item function to largerArray.extractArray(result)
work correctly as I am trying to run the code and gave a window error, not a compile error.
Does anyone know how to retrieve the content smallerArray.extractPiece()
and instead of getting the address, and does it have any other methods to create a 2D array object?
void Grid::extractArray( int** arr )
{
for(int i = 0; i < xGrid ; ++i) {
for (int j = 0; j < yGrid ; ++j) {
squares[i][j] = arr[i][j];
}
}
}
A smaller array int**arr
doesn't have as many elements as Grid
.
xGrid
and are yGrid
too large to be used as indexes for arr[][]
.
You must pass the complete smaller array object to the function extractArray()
and use the dimensions of that object for the copy function.
void Grid::extractArray( const Piece & piece)
{
for(int i = 0; i < piece.xGrid ; ++i) {
for (int j = 0; j < piece.yGrid ; ++j) {
squares[i][j] = arr[i][j];
}
}
}
source to share
It can make it easier if you create a 2D array object or class (or structure)
class BaxMatrix {
public:
int m_Data[4][4];
}
with a little work, you could build dynamic structures or use STL structures as you like. Data and data link are two different animals. It is best for you to clarify each of your roles in your thinking before continuing.
source to share