【问题标题】:Porting C++ Arrays to C# Arrays将 C++ 数组移植到 C# 数组
【发布时间】:2012-03-04 05:20:51
【问题描述】:

我正在将此 SimplexNoise 模块从 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);

对于我得到的那一行,无法从 int[] 转换为 int。这是合乎逻辑的,但这在 C++ 版本中怎么没有任何错误?我只知道 C++ 的基础知识,但据我所知,这是尝试用一维 int 数组分配一个整数变量,这没有任何意义。

有什么想法吗?

【问题讨论】:

  • dot()your 版本是什么样的?这就是问题所在。

标签: c# c++ arrays multidimensional-array porting


【解决方案1】:

这是因为根据您链接的来源,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);

【讨论】:

    【解决方案2】:

    试试这个:

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

    我建议您也阅读这篇关于将 C++ 移植到 C# 的 MSDN 文章(虽然它可能有点过时):http://msdn.microsoft.com/en-us/magazine/cc301520.aspx

    【讨论】:

    • 这个答案似乎与问题无关。
    猜你喜欢
    • 2011-09-28
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2019-07-18
    • 1970-01-01
    相关资源
    最近更新 更多