In a C header file included in a C++ project, I have functions that take in arrays of passed-in sizes. The function prototypes show errors when compiling in a C++ environment, but they compiled without issues in the past. Here is the the header file and the corresponding C file. I am compiling with GCC. All of the arrays that are passed into the functions are known sizes and are allocated on the stack. What could be causing these errors?
Sample of Matrix.h:
#ifndef MATRIX_H
#define MATRIX_H
#ifdef __cplusplus
extern "C" {
#endif
#include "stdint.h"
#include "stdbool.h"
void matrixAdd(uint32_t r, uint32_t c,
float mat1[r][c], const float mat2[r][c]);
void matrixMultiply(uint32_t r1, uint32_t c1, uint32_t r2, uint32_t c2,
const float mat1[r1][c1], const float mat2[r2][c2],
float mat0[r1][c2]);
void matrixTranspose(uint32_t r1, uint32_t c1, const float mat1[r1][c1],
float matT[c1][r1]);
bool matrixInverse(uint32_t s, const float mat1[s][s], float matI[s][s]);
void matrixCoefMultiply(uint32_t r, uint32_t c, float mat1[r][c], float);
#ifdef __cplusplus
}
#endif
#endif
Gives the errors:
In file included from src/GNC.h:10,
from src/main.cpp:10:
src/Matrix.h:38:28: error: use of parameter outside function body before ']' token
38 | float mat1[r][c], const float mat2[r][c]);
| ^
src/Matrix.h:38:31: error: use of parameter outside function body before ']' token
38 | float mat1[r][c], const float mat2[r][c]);
| ^
src/Matrix.h:38:32: error: expected ')' before ',' token
38 | float mat1[r][c], const float mat2[r][c]);
| ^
| )
src/Matrix.h:37:15: note: to match this '('
37 | void matrixAdd(uint32_t r, uint32_t c,
| ^
src/Matrix.h:38:34: error: expected unqualified-id before 'const'
38 | float mat1[r][c], const float mat2[r][c]);
| ^~~~~
src/Matrix.h:41:40: error: use of parameter outside function body before ']' token
41 | const float mat1[r1][c1], const float mat2[r2][c2],
| ^
src/Matrix.h:41:44: error: use of parameter outside function body before ']' token
41 | const float mat1[r1][c1], const float mat2[r2][c2],
| ^
src/Matrix.h:41:45: error: expected ')' before ',' token
41 | const float mat1[r1][c1], const float mat2[r2][c2],
| ^
| )
src/Matrix.h:40:20: note: to match this '('
40 | void matrixMultiply(uint32_t r1, uint32_t c1, uint32_t r2, uint32_t c2,
| ^
src/Matrix.h:41:47: error: expected unqualified-id before 'const'
41 | const float mat1[r1][c1], const float mat2[r2][c2],
| ^~~~~
src/Matrix.h:44:67: error: use of parameter outside function body before ']' token
44 | void matrixTranspose(uint32_t r1, uint32_t c1, const float mat1[r1][c1],
| ^
src/Matrix.h:44:71: error: use of parameter outside function body before ']' token
44 | void matrixTranspose(uint32_t r1, uint32_t c1, const float mat1[r1][c1],
| ^
src/Matrix.h:44:72: error: expected ')' before ',' token
44 | void matrixTranspose(uint32_t r1, uint32_t c1, const float mat1[r1][c1],
| ~ ^
| )
src/Matrix.h:45:22: error: expected unqualified-id before 'float'
45 | float matT[c1][r1]);
| ^~~~~
src/Matrix.h:47:50: error: use of parameter outside function body before ']' token
47 | bool matrixInverse(uint32_t s, const float mat1[s][s], float matI[s][s]);
| ^
src/Matrix.h:47:53: error: use of parameter outside function body before ']' token
47 | bool matrixInverse(uint32_t s, const float mat1[s][s], float matI[s][s]);
| ^
src/Matrix.h:47:54: error: expected ')' before ',' token
47 | bool matrixInverse(uint32_t s, const float mat1[s][s], float matI[s][s]);
| ~ ^
| )
src/Matrix.h:47:56: error: expected unqualified-id before 'float'
47 | bool matrixInverse(uint32_t s, const float mat1[s][s], float matI[s][s]);
| ^~~~~
src/Matrix.h:49:61: error: use of parameter outside function body before ']' token
49 | void matrixCoefMultiply(uint32_t r, uint32_t c, float mat1[r][c], float);
| ^
src/Matrix.h:49:64: error: use of parameter outside function body before ']' token
49 | void matrixCoefMultiply(uint32_t r, uint32_t c, float mat1[r][c], float);
| ^
src/Matrix.h:49:65: error: expected ')' before ',' token
Compiling .pio/build/adafruit_itsybitsy_m4/lib47f/Adafruit_TinyUSB_Arduino/class/cdc/cdc_device.c.o
49 | void matrixCoefMultiply(uint32_t r, uint32_t c, float mat1[r][c], float);
| ~ ^
| )
src/Matrix.h:49:67: error: expected unqualified-id before 'float'
49 | void matrixCoefMultiply(uint32_t r, uint32_t c, float mat1[r][c], float);
| ^~~~~
*** [.pio/build/adafruit_itsybitsy_m4/src/main.cpp.o] Error 1
The functions are used like the following:
void foo(void)
{
float A[2][2] = {
{1, 0},
{0, 1}
};
float B[2][2] = {
{1, 1},
{0, 1}
};
matrixAdd(2, 2, A, B);
/* Do other stuff */
}