0

I have

void createMat(int N, double X[][N], double Y[][N]){
        for (int x = 0; x < (N / 2); x++) {
                for (int y = 0; y < (N / 2); y++) {
                    X[x][y] = Y[x][y];
                }
            }
    }

and call it in main

createMat(N,A,a);

I do not understand, what is wrong. I create matrix, N is number of rows and columns put by user. A is new matrix, a is the old one.

**main.cpp:19:35: error: use of parameter outside function body before ']' token
 void createMat(int N, double X[][N], double Y[][N]){
                                   ^
main.cpp:19:36: error: expected ')' before ',' token
 void createMat(int N, double X[][N], double Y[][N]){
                                    ^
main.cpp:19:38: error: expected unqualified-id before 'double'
 void createMat(int N, double X[][N], double Y[][N]){**

1 Answers1

0
**main.cpp:19:35: error: use of parameter outside function body before ']' token
void createMat(int N, double X[][N], double Y[][N]){
                                ^

The error message is pretty clear. You cannot use N at the point of parameter declarations to specify a fixed array size. The error message tells you that in general, you cannot use the parameter N outside of the functions body introduced with the {} curly braces.

Note:
C++ doesn't support variable length arrays.

Use a std::vector<std::vector<double>> as parameter type instead, or a std::array<std::array<double, N>, M> where N and M are known values at compile time.

  • All my app is based on double[][]. How can I put it to a func? – Prazhak Jan 13 '18 at 15:58
  • Using raw arrays was probably a bad idea. Your app should be based on using the `std::vector` or `std::array` types as I mentioned. May be you can manage to do something with `double[][]` without specifying the row size at the parameter declaration. I won't recommend to do that though.# –  Jan 13 '18 at 16:07
  • Can I use threads for such for loops? for (int x = 0; x < (N / 2); x++) { for (int y = 0; y < (N / 2); y++) { A[x][y] = a[x][y]; } } – Prazhak Jan 13 '18 at 16:10
  • @Prazhak What have _threads_ to do with your question actually? You can use `std::thread` in for loops, yes. –  Jan 13 '18 at 16:13
  • I must use threads, but I dont know how. I read it must be through function. – Prazhak Jan 13 '18 at 16:19
  • @Prazhak That's completely beyond of your original question about the error message, and using raw arrays as function parameters has absolutely nothing to do with using threads or not. –  Jan 13 '18 at 16:23