C# - 错误CS0123方法或委托参数与委托参数

时间:2015-09-20 11:37:03

标签: c# unity3d types delegates

我遇到了一个我能理解但无法修复的错误,它是一个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'无法转换对象'表达式键入' System.Collections.Generic.IEnumerable'

我很确定两个持续时间只是因为第一个因为它不能识别应该是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;
    }
    }

2 个答案:

答案 0 :(得分:0)

  

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

当你尝试传递calcDistance并且如果你看到预期的输入是Func<node> estimate这意味着它正在寻找一个返回类型的委托时,错误似乎有很多错误Node。显然,您的calcDistance是一个功能而不是代表。

因此错误。

答案 1 :(得分:-1)

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

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