Here is the declaration of the constructor I’m using:
_Task Multiplier {
int **Z;
int **X;
int **Y;
int xr, xcols_yrows, yc;
void main() {
for( int i = 0; i < xcols_yrows; i++ ) {
matrixmultiply(Z, X, xr, i, Y, yc);
}
}
public:
Multiplier( int *Z[], int *X[], int *Y[], int xr, int xcols_yrows, int yc) :
Z( Z ), X( X ), Y( Y ), xr( xr ), xcols_yrows( xcols_yrows ), yc( yc ) {}
};
And here is where it is used:
int xrows, xcols_yrows, ycols;
// [cols][rows]
int X[xrows][xcols_yrows], Y[xcols_yrows][ycols], Z[xrows][ycols];
// start threads to multiply rows
Multiplier *multipliers[xrows];
for( int r = 0; r < xrows; r++ ) {
multipliers[r] = new Multiplier( &Z, &X, &Y, r, xcols_yrows, ycols );
}
(they are all initialized)
But I get this weird error:
q3.cc: In member function 'virtual void uMain::main()':
q3.cc:132: error: no matching function for call to 'Multiplier::Multiplier(int (*)[(((unsigned int)(((int)xrows) + -0x00000000000000001)) + 1)][(((unsigned int)(((int)ycols) + -0x00000000000000001)) + 1)], int (*)[(((unsigned int)(((int)xrows) + -0x00000000000000001)) + 1)][(((unsigned int)(((int)xcols_yrows) + -0x00000000000000001)) + 1)], int (*)[(((unsigned int)(((int)xcols_yrows) + -0x00000000000000001)) + 1)][(((unsigned int)(((int)ycols) + -0x00000000000000001)) + 1)], int&, int&, int&)'
q3.cc:37: note: candidates are: Multiplier::Multiplier(int**, int**, int**, int, int, int, UPP::uAction)
q3.cc:26: note: Multiplier::Multiplier(Multiplier&)
make: *** [q3.o] Error 1
The problem is that the function parameters
X,YandZbasically expect aarray of pointers to int, but you’re giving it apointer to an array of arrays of int.Specifying a parameter as an array (declared with
[]) decays to declaring a parameter as a pointer. And this is ok, as an array variable can be basically treated as a pointer to the first element of the array.This is not valid for n-dimensional arrays, though, as the compiler needs to know how wide the array is along the second dimension in order to calculate the start of the next element along the first dimension… that is, in order to know what location in memory
X[1][0]refers to, the compiler needs to know how many elements are inX[0].One solution would be to use a pointer to the start of each matrix, and do the address calculations yourself. Such as:
And know that
X[i][j]in the calling code is actuallyX[i*xcols_yrows+j]insideMultiplier.