Search code examples
c++matrixsymmetric

error: cannot convert 'float (*)[(((sizetype)(((ssizetype)n) + -1)) + 1)]' to 'float (*)[100]' for argument '3'


I'm trying to do a boolean function which verifies if a matrix is symmetric, but I'm getting this error:

|54|error: cannot convert 'float ()[(((sizetype)(((ssizetype)n) + -1)) + 1)]' to 'float ()[100]' for argument '3' to 'void Transpose(int, float ()[100], float ()[100])'|

#include <iostream>
using namespace std;

void Transpose(int n, float a[][MAX], float T[][MAX]) {
    int i,j;

    for(i = 0; i < n; i++){
        for(j = 0; j < n; j++){
            T[i][j] = a[j][i];
        }
    }
}

bool Symmetric(int n, float a[][MAX]) {
    float t[n][n];
    int i,j;

    for(i = 0; i < n; i++){
        for(j = 0; j < n; j++){
            t[i][j] = 0;
        }
    }

    Transpose(n,a,t); // <--- Error here.

    for(i = 0; i < n; i++){
        for(j = 0; j < n; j++){
            if(t[i][j] != a[i][j]){
                return false;
            }
        }
    }

    return true;
}

The error happens in the Transpose(n,a,t); line


Solution

  • What the compiler says is that an array of float t[n][n] where n is a compile-time variable [that is, not a constant] does not match float T[][MAX], when MAX is a compile-time constant.

    It will probably work fine to use float t[n][MAX] for your temporary matrix. However, bear in mind that C and C++ does not deal with "variable size arrays" (in particular when passing them from one function to another) very well, and in C++, it would probably be a better idea to use a different way to describe your matrix. For example:

    std::vector<vector<float>> t; 
    

    You will then need to do some more work to define the size of the vector, for example:

    t.resize(n);
    for(i = 0; i < n; i++){
       t[i].resize(n);