2012-03-04 112 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