I am essentially trying to write a function that acts as np.reshape, but in C++. I want to take an input matrix, define the dimensions of the output matrix, and have the code create an output array of the appropriate size. I am working in Microsoft Visual Studio 2019, and have changed over to the ISO C++ 17 Standard. I know that C++ was not designed to support variable length arrays, but am trying to find a workaround.
int8_t reshape(int8_t **inputMatrix, int outputRows, int outputCols) {
int inputRows = sizeof(inputMatrix) / sizeof(inputMatrix[0]);
int inputCols = sizeof(inputMatrix[0]);
int ii = 0; int jj = 0;
int8_t outputMatrix[outputRows][outputCols];
for (int i = 0; i < inputRows; i++) {
for (int j = 0; j < inputCols; j++) {
outputMatrix[ii][jj] = inputMatrix[i][j];
jj++;
if (jj == outputCols) {
jj = 0;
ii++;
}
}
}
return outputMatrix;
}
I am receiving the following errors:
- In the line int8_t outputMatrix[outputRows][outputCols];, I am getting a double error of "E0028 - expression must have a constant value" in reference to the variables outputRows and outputCols.
- In the same line, there is another double error "C2131 - expression did not evaluate to a constant"
- In the line outputMatrix[ii][jj] = inputMatrix[i][j]; there is an error "C3863 - array type 'int8_t [outputRows][outputCols]' is not assignable"
In the final purpose for this code it would be okay to use malloc/calloc, but I haven't been able to make that work.