![](/img/trans.png)
[英]How can I fix this error - C# CS0123: No overload for “method” matches delegate 'EventHandler'
[英]C# - error CS0123 A method or delegate parameters do not match delegate parameter
我陷入了我可以理解但無法解決的錯誤,這是CS0123錯誤,其中方法和委托之間的參數不匹配。 但是我查看了我的代碼,它似乎是正確的類型,因此我很困惑,並尋求您的幫助,因為我正在通過該項目學習更高級的C#。 此項目將要生成一個十六進制網格,然后在兩個十六進制圖塊之間對其進行A *尋路。 我使用了這一系列書面教程,即使它是舊的,也不得不刷新一些代碼以使其在Unity 5和C#.NET的最新版本上工作(我猜)。
這是錯誤:
資產/腳本/GridManager.cs(151,39):錯誤CS0123:方法或委托'GridManager.calcDistance(Tile)'參數與委托'System.Func()'參數不匹配
資產/腳本/GridManager.cs(153,17):錯誤CS1502:GridManager.DrawPath(System.Collections.Generic.IEnumerable)的最佳重載方法匹配具有一些無效的參數
資產/腳本/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;
}
}
var path = PathFinder.FindPath(originTileTB.tile,destTileTB.tile,distance,calcDistance); //錯誤在這里
好吧,似乎有很多錯誤,恰恰是當您嘗試傳遞calcDistance
時出現錯誤,並且如果您看到期望的輸入,則它是Func<node> estimate
,這意味着它正在尋找返回Node
類型的委托。 顯然,您的calcDistance
是一個函數而不是委托。
因此錯誤。
在FindPath方法中,您將估算值定義為不帶任何參數並返回節點的Func,但是在調用該方法時,您傳入的是CalcDistance,該值使用一個圖塊並返回一個double。 看起來估計應該是一個接受Node並返回雙精度值的Func。
public static Path<Node> FindPath<Node>(
Node start,
Node destination,
Func<Node, Node, double> distance,
Func<Node, double> estimate)
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.