我刚开始学习如何编程并一直关注此tutorial on graphs,但我使用给定的
遇到了一些问题AddDirectedEdge(GraphNode<T> from, GraphNode<T> to, int cost)
使用教程中的默认值无法构建,例如
web.AddDirectedEdge("People.aspx", "Privacy.htm"); // People -> Privacy
这给出了错误&#34;方法&#39; AddDirectedEdge&#39;需要2个参数&#34; 。添加一个整数作为第三个参数也没有帮助,它只是给出了另一个错误&#34;参数#:无法转换为&#39; string&#39; to&#39; GraphTest.GraphNode&lt;串GT;&#39;&#34;
我不确定如何解决此问题,任何有关它的帮助/见解将不胜感激。为便于查看,图形和图形节点类如下所示(这些也可通过上述链接获得):
public class GraphNode<T> : Node<T>
{
private List<int> costs;
public GraphNode() : base() { }
public GraphNode(T value) : base(value) { }
public GraphNode(T value, NodeList<T> neighbors) : base(value, neighbors) { }
new public NodeList<T> Neighbors
{
get
{
if (base.Neighbors == null)
base.Neighbors = new NodeList<T>();
return base.Neighbors;
}
}
public List<int> Costs
{
get
{
if (costs == null)
costs = new List<int>();
return costs;
}
}
}
public class Graph<T> : IEnumerable<T>
{
private NodeList<T> nodeSet;
public Graph() : this(null) { }
public Graph(NodeList<T> nodeSet)
{
if (nodeSet == null)
this.nodeSet = new NodeList<T>();
else
this.nodeSet = nodeSet;
}
public void AddNode(GraphNode<T> node)
{
// adds a node to the graph
nodeSet.Add(node);
}
public void AddNode(T value)
{
// adds a node to the graph
nodeSet.Add(new GraphNode<T>(value));
}
public void AddDirectedEdge(GraphNode<T> from, GraphNode<T> to, int cost)
{
from.Neighbors.Add(to);
from.Costs.Add(cost);
}
public void AddUndirectedEdge(GraphNode<T> from, GraphNode<T> to, int cost)
{
from.Neighbors.Add(to);
from.Costs.Add(cost);
to.Neighbors.Add(from);
to.Costs.Add(cost);
}
public bool Contains(T value)
{
return nodeSet.FindByValue(value) != null;
}
public bool Remove(T value)
{
// first remove the node from the nodeset
GraphNode<T> nodeToRemove = (GraphNode<T>)nodeSet.FindByValue(value);
if (nodeToRemove == null)
// node wasn't found
return false;
// otherwise, the node was found
nodeSet.Remove(nodeToRemove);
// enumerate through each node in the nodeSet, removing edges to this node
foreach (GraphNode<T> gnode in nodeSet)
{
int index = gnode.Neighbors.IndexOf(nodeToRemove);
if (index != -1)
{
// remove the reference to the node and associated cost
gnode.Neighbors.RemoveAt(index);
gnode.Costs.RemoveAt(index);
}
}
return true;
}
public NodeList<T> Nodes
{
get
{
return nodeSet;
}
}
public int Count
{
get { return nodeSet.Count; }
}
public IEnumerator<T> GetEnumerator()
{
throw new NotImplementedException();
}
System.Collections.IEnumerator System.Collections.IEnumerable.GetEnumerator()
{
throw new NotImplementedException();
}
IEnumerator<T> IEnumerable<T>.GetEnumerator()
{
throw new NotImplementedException();
}
}
答案 0 :(得分:2)
嗯,显然它抱怨类型不匹配:
您提供string
类型而不是GraphNode<T>
。
相反,你应该把它称为:
web.AddDirectedEdge(new GraphNode("People.aspx"), new GraphNode("Privacy.htm"), 1);
其中1
是费用,是必需的,或
var ppl = new GraphNode("People.aspx");
var prv = new GraphNode("Privacy.htm");
web.AddDirectedEdge(ppl, prv, 1);
如果您希望在代码中进一步重用节点。
作为旁注,似乎你正在犯一些微不足道的错误。在进一步说明之前,我强烈建议您至少参加C#的基础课程。网上有大量免费提供,this for example。