简体   繁体   中英

Trouble with converting function calls involving arrays in C++ to C#

I needed to convert the following C++ code into C#. I do not know which C++ version I was given. I used the converter from Tangible Software to convert method by method from C++ into C#. This Multiply1x3 method is being called from two places as shown below and is giving me a bit of trouble after converting into C#. Can you tell me where I am going wrong?

void Conversions::Multiply1x3(double matr[3], double matr3[3][3], double result[3])
{
    int i;
    for (i = 0; i < 3; i++)
    {
        result[i] = matr[0] * matr3[0][i] + matr[1] * matr3[1][i] + matr[2] * matr3[2][i];
    }
}

void Conversions::SomeMethod2(double matr1[3][3], double matr2[3][3], double result[3][3])
{
    double tempArray[3];
    for (i = 0; i < 3; i++) 
    {
        Multiply1x3(tempArray, matr2, result[i]);
    }
}

void Conversions::SomeMethod2() 
{
    double matr[3][3];
    double source[3];
    double NewX[3];
    Multiply1x3(source, matr, NewX);
}

I get the following C# code after conversion:

public partial class Conversions
{
    public void Multiply1x3(double[] matr, double[,] matr3, double[] result)
    {
        int i;
        for (i = 0; i < 3; i++)
        {
            result[i] = matr[0] * matr3[0, i] + matr[1] * matr3[1, i] + matr[2] * matr3[2, i];
        }
    }
    public void SomeMethod2(double[,] matr1, double[,] matr2, double[,] result)
    {
        double[] tempArray = new double[3];
        for (i = 0; i < 3; i++)
        {
            Multiply1x3(tempArray, matr2, result[i]); // ERROR
        }
    }
    public void SomeMethod2()
    {
        double[,] matr = new double[3, 3];
        double[] source = new double[3];
        double[] NewX = new double[3];
        Multiply1x3(source, matr, NewX);
    }
}

Compiling this C# code gives me the following errors, all 3 from the same line:

Argument 3: cannot convert from 'double' to 'double[]'
Wrong number of indices inside []; expected 2
The best overloaded method match for 'Project.Conversions.Multiply1x3(double[], double[*,*], double[])' has some invalid arguments

The problem is that C# does not support accessing slices of multidimensional arrays; ie, from your two-dimensional array result you are attempting to obtain a reference to the i th row by result[i] , but C# simply does not support this operation. One workaround would be to use the jagged array type double[][] in place of the multidimensional array type double[,] .

Multiply1x3 takes a double[] :

(..., double[,] matr3, double[] result)
//                     ^^^^^^^^^^^^^^^

But you passed it a double as result[i] returns a double.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM