2012-03-04 199 views
1

我正在将此模块从C++移植到C#,并且遇到了程序员从数组中取回值的方式问题。他有类似如下:将C++数组移植到C#数组

simplexnoise.h

static const int grad3[12][3] = { 
    {1,1,0}, {-1,1,0}, {1,-1,0}, {-1,-1,0}, 
    {1,0,1}, {-1,0,1}, {1,0,-1}, {-1,0,-1}, 
    {0,1,1}, {0,-1,1}, {0,1,-1}, {0,-1,-1} 
}; 

simplesxnoise.cpp

n1 = t1 * t1 * dot(grad3[gi1], x1, y1); 

在我的C#端口:

SimplexNoise.cs

private static int[][] grad3 = new int[][] { new int[] {1,1,0}, new int[] {-1,1,0}, new int[] {1,-1,0}, new int[] {-1,-1,0}, 
               new int[] {1,0,1}, new int[] {-1,0,1}, new int[] {1,0,-1}, new int[] {-1,0,-1}, 
               new int[] {0,1,1}, new int[] {0,-1,1}, new int[] {0,1,-1}, new int[] {0,-1,-1}}; 

... 

    n1 = t1 * t1 * dot(grad3[gi1], x1, y1); 

而fo r我得到的那一行,不能从int []转换为int。这是合乎逻辑的,但是它在C++版本中没有任何错误?我只知道C++的基础知识,但从我所知道的是试图给一个1D int数组赋予一个整型变量,这只是没有任何意义。

任何想法?

+0

dot()的_your_版本的外观如何?这将是问题。 – 2012-03-04 10:53:58

回答

2

这是因为根据您链接的源,dot()期望的阵列的第一个参数:

float dot(const int* g, const float x, const float y); 

const int* g的意思是“一个指针的整数”或“一个整数数组”。考虑到使用情况,它是签名所暗示的“整数数组”。因此,你需要改变你的C#dot()的签名:

float dot(int g[], float x, float y); 
1

试试这个:

int grad3[,] = { 
       {1,1,0}, {-1,1,0}, {1,-1,0}, {-1,-1,0}, 
       {1,0,1}, {-1,0,1}, {1,0,-1}, {-1,0,-1}, 
       {0,1,1}, {0,-1,1}, {0,1,-1}, {0,-1,-1} 
       }; 

我建议你也读这个MSDN文章(虽然它可能会有点过时)上将C++移植到C#中:http://msdn.microsoft.com/en-us/magazine/cc301520.aspx

+0

这个答案似乎与问题无关。 – 2012-03-04 05:50:21