【问题标题】:C# - error CS0123 A method or delegate parameters do not match delegate parameterC# - 错误 CS0123 方法或委托参数与委托参数不匹配
【发布时间】:2015-12-17 04:36:37
【问题描述】:

我遇到了一个我可以理解但无法修复的错误,这是一个 CS0123 错误,其中方法和委托之间的参数不匹配。但是我查看了我的代码,它似乎是正确的类型,所以我很困惑并寻求您的帮助,因为我正在通过这个项目学习更高级的 C#。该项目即将生成一个六角网格,然后在两个六角图块之间对其进行 A* 寻路。我使用了this serie of written tutorials,即使它是旧的,并且必须刷新一点代码才能使其在 Unity 5 和更新版本的 C# .NET 上工作(我猜)。

以下是错误:

Assets/Scripts/GridManager.cs(151,39):错误 CS0123:方法或委托“GridManager.calcDistance(Tile)”参数与委托“System.Func()”参数不匹配

Assets/Scripts/GridManager.cs(153,17):错误 CS1502:“GridManager.DrawPath(System.Collections.Generic.IEnumerable)”的最佳重载方法匹配有一些无效参数

Assets/Scripts/GridManager.cs(153,17):错误 CS1503:参数“#1”无法将“object”表达式转换为“System.Collections.Generic.IEnumerable”类型

我很确定两个 last 的存在只是因为第一个因为它无法识别应该是 Tile 的正确 var 类型。

我希望你能帮助我并解释我做错了什么。我想我不完全是那里发生的事情。

提前谢谢你!

这是我修改过的一些代码,但和教程基本一样:

GridManager.cs:

    double calcDistance(Tile tile)
    {
            Tile destTile = destTileTB.tile;
            float deltaX = Mathf.Abs(destTile.X - tile.X);
            float deltaY = Mathf.Abs(destTile.Y - tile.Y);
            int z1 = -(tile.X + tile.Y);
            int z2 = -(destTile.X + destTile.Y);
            float deltaZ = Mathf.Abs(z2 - z1);

            return Mathf.Max(deltaX, deltaY, deltaZ);
    }

    private void DrawPath(IEnumerable<Tile> path)
    {
            if (this.path == null)
                    this.path = new List<GameObject>();
            this.path.ForEach(Destroy);
            this.path.Clear();

            GameObject lines = GameObject.Find("Lines");
            if (lines == null)
                    lines = new GameObject("Lines");
            foreach (Tile tile in path)
            {
                    var line = (GameObject)Instantiate(Line);
                    Vector2 gridPos = new Vector2(tile.X + tile.Y / 2, tile.Y);
                    line.transform.position = calcWorldCoord(gridPos);
                    this.path.Add(line);
                    line.transform.parent = lines.transform;
            }
    }

    public void generateAndShowPath()
    {
            if (originTileTB == null || destTileTB == null)
            {
                    DrawPath(new List<Tile>());
                    return;
            }
            Func<Tile, Tile, double> distance = (node1, node2) => 1;

            var path = PathFinder.FindPath(originTileTB.tile, destTileTB.tile,
                                           distance, calcDistance); //error is here
            DrawPath(path);
    }

PathFinder.cs:

    using System;
    using System.Collections;
    using System.Collections.Generic;
    using System.Linq;

    public static class PathFinder
    {
    public static Path<Node> FindPath<Node>(
            Node start,
            Node destination,
            Func<Node, Node, double> distance,
            Func<Node> estimate)
            where Node : IHasNeighbours<Tile>
    {
            var closed = new HashSet<Node>();
            var queue = new PriorityQueue<double, Path<Node>>();
            queue.Enqueue(0, new Path<Node>(start));

            while (!queue.IsEmpty)
            {
                    var path = queue.Dequeue();

                    if (closed.Contains(path.LastStep))
                            continue;
                    if (path.LastStep.Equals(destination))
                            return path;

                    closed.Add(path.LastStep);

                    foreach (Node n in path.LastStep.Neighbours)
                    {
                            double d = distance(path.LastStep, n);
                            var newPath = path.AddStep(n, d);
                            queue.Enqueue(newPath.TotalCost + estimate(n), newPath);
                    }
            }

            return null;
    }
    }

【问题讨论】:

    标签: c# unity3d types delegates


    【解决方案1】:

    var path = PathFinder.FindPath(originTileTB.tile, destTileTB.tile, 距离,计算距离); //错误在这里

    似乎有很多错误,确切地说,错误是当您尝试传递calcDistance 时,如果您看到预期的输入是Func&lt;node&gt; estimate,这意味着它正在寻找返回类型Node 的委托。显然你的 calcDistance 是一个函数而不是一个委托。

    因此出现错误。

    【讨论】:

    • 我需要更多的工作代表,但多亏了这一点,我对这个主题有了更多的了解。谢谢你的回答!
    【解决方案2】:

    在 FindPath 方法中,您将estimate 定义为不带参数并返回节点的 Func,但在调用该方法时,您传入 calcDistance,该方法采用 tile 并返回 double。看起来估计应该是一个接受节点并返回双精度的 Func。

    public static Path<Node> FindPath<Node>(
            Node start,
            Node destination,
            Func<Node, Node, double> distance,
            Func<Node, double> estimate)
    

    【讨论】:

    • 它有效,但在其他任何地方都显示了很多错误。我设法修复它,现在一切都很好。所以谢谢 ! :)
    猜你喜欢
    • 2015-04-22
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2014-12-10
    • 2013-01-25
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多