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

StackOverflow https://stackoverflow.com/questions/22121974

  •  18-10-2022
  •  | 
  •  

Frage

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

War es hilfreich?

Lösung

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);

Andere Tipps

What you can do is replace those arrays with std::vector< vector < float >>, as the other answers have stated. I would use a typedef to make things a little simpler. In addition, once you start using vector, take advantage of it in other ways such as the easier initialization.

#include <vector>
typedef std::vector<float> Float1D;
typedef std::vector<Float1D> Float2D;

void Transpose(int n, const Float2D& a, Float2D& T) 
{
    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, Float2D& a) 
{
    Float2D t(n, Float1D(n,0));
    Transpose(n,a,t);
    // assuming that a and t are the same size matrix
    return t == a;
}

Note the initialization of the t vector in the Symmetric function. No loops were needed as all that was done was declare it with a row size of n, and initialize each row with a 1-dimensional float vector, with each entry in the 1-d array initialized to 0.

Also note the test at the end. All I did was compare if the vectors have equal components using operator ==.

With the code above, I'm assuming your matrices are the same size. If not, you will need to put checks to ensure this (I didn't do that).

Lizenziert unter: CC-BY-SA mit Zuschreibung
Nicht verbunden mit StackOverflow
scroll top