gpt4 book ai didi

c# - msdn C# 图形示例中的无效参数错误

转载 作者:行者123 更新时间:2023-12-02 10:46:32 25 4
gpt4 key购买 nike

我正在尝试按照位于此地址的 Microsoft Developer Network 上的教程在 C# 中构建图形数据结构:https://msdn.microsoft.com/en-us/library/ms379574(v=vs.80).aspx .本教程继续上一课关于二叉树的内容:https://msdn.microsoft.com/en-us/library/ms379572(v=vs.80).aspx .我使用二叉树类(class)中的 Node 类和 NodeList 类来构建这个图。在这两个类(class)之后,我还使用了创建图表类(class)中介绍的代码。这是我试图在 https://repl.it/languages/csharp 上运行的代码:

`

using System;
using System.Collections;
using System.Collections.ObjectModel;
using System.Collections.Generic;

public class Node<T>
{
// Private member-variables
private T data;
private NodeList<T> neighbors = null;

public Node() {}
public Node(T data) : this(data, null) {}
public Node(T data, NodeList<T> neighbors)
{
this.data = data;
this.neighbors = neighbors;
}

public T Value
{
get
{
return data;
}
set
{
data = value;
}
}

protected NodeList<T> Neighbors
{
get
{
return neighbors;
}
set
{
neighbors = value;
}
}
}

public class NodeList<T> : Collection<Node<T>>
{
public NodeList() : base() { }

public NodeList(int initialSize)
{
// Add the specified number of items
for (int i = 0; i < initialSize; i++)
base.Items.Add(default(Node<T>));
}

public Node<T> FindByValue(T value)
{
// search the list for the value
foreach (Node<T> node in Items)
if (node.Value.Equals(value))
return node;

// if we reached here, we didn't find a matching node
return null;
}
}

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) {}

/* IEnumerator IEnumerable.GetEnumerator()
{
// call the generic version of the method
return System.Collection.IEnumerable.GetEnumerator();
}*/

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


class MainClass {
public static void Main (string[] args) {
Graph<string> web = new Graph<string>();
web.AddNode("Privacy.htm");
web.AddNode("People.aspx");
web.AddNode("About.htm");
web.AddNode("Index.htm");
web.AddNode("Products.aspx");
web.AddNode("Contact.aspx");

web.AddDirectedEdge("People.aspx", "Privacy.htm"); // People -> Privacy

web.AddDirectedEdge("Privacy.htm", "Index.htm"); // Privacy -> Index
web.AddDirectedEdge("Privacy.htm", "About.htm"); // Privacy -> About

web.AddDirectedEdge("About.htm", "Privacy.htm"); // About -> Privacy
web.AddDirectedEdge("About.htm", "People.aspx"); // About -> People
web.AddDirectedEdge("About.htm", "Contact.aspx"); // About -> Contact

web.AddDirectedEdge("Index.htm", "About.htm"); // Index -> About
web.AddDirectedEdge("Index.htm", "Contact.aspx"); // Index -> Contacts
web.AddDirectedEdge("Index.htm", "Products.aspx"); // Index -> Products

web.AddDirectedEdge("Products.aspx", "Index.htm"); // Products -> Index
web.AddDirectedEdge("Products.aspx", "People.aspx");// Products -> People
}
}

`

但是,当我尝试在 https://repl.it/languages/csharp 上运行代码时它给了我以下错误:
main.cs(202,5): error CS1502: The best overloaded method match for `Graph<string>.AddDirectedEdge(GraphNode<string>, GraphNode<string>)' has some invalid arguments
main.cs(131,17): (Location of the symbol related to previous error)
main.cs(202,21): error CS1503: Argument `#1' cannot convert `string' expression to type `GraphNode<string>'
main.cs(204,5): error CS1502: The best overloaded method match for `Graph<string>.AddDirectedEdge(GraphNode<string>, GraphNode<string>)' has some invalid arguments
main.cs(131,17): (Location of the symbol related to previous error)
main.cs(204,21): error CS1503: Argument `#1' cannot convert `string' expression to type `GraphNode<string>'
main.cs(205,5): error CS1502: The best overloaded method match for `Graph<string>.AddDirectedEdge(GraphNode<string>, GraphNode<string>)' has some invalid arguments
main.cs(131,17): (Location of the symbol related to previous error)
main.cs(205,21): error CS1503: Argument `#1' cannot convert `string' expression to type `GraphNode<string>'
main.cs(207,5): error CS1502: The best overloaded method match for `Graph<string>.AddDirectedEdge(GraphNode<string>, GraphNode<string>)' has some invalid arguments
main.cs(131,17): (Location of the symbol related to previous error)
main.cs(207,21): error CS1503: Argument `#1' cannot convert `string' expression to type `GraphNode<string>'
main.cs(208,5): error CS1502: The best overloaded method match for `Graph<string>.AddDirectedEdge(GraphNode<string>, GraphNode<string>)' has some invalid arguments
main.cs(131,17): (Location of the symbol related to previous error)
main.cs(208,21): error CS1503: Argument `#1' cannot convert `string' expression to type `GraphNode<string>'
main.cs(209,5): error CS1502: The best overloaded method match for `Graph<string>.AddDirectedEdge(GraphNode<string>, GraphNode<string>)' has some invalid arguments
main.cs(131,17): (Location of the symbol related to previous error)
main.cs(209,21): error CS1503: Argument `#1' cannot convert `string' expression to type `GraphNode<string>'
main.cs(211,5): error CS1502: The best overloaded method match for `Graph<string>.AddDirectedEdge(GraphNode<string>, GraphNode<string>)' has some invalid arguments
main.cs(131,17): (Location of the symbol related to previous error)
main.cs(211,21): error CS1503: Argument `#1' cannot convert `string' expression to type `GraphNode<string>'
main.cs(212,5): error CS1502: The best overloaded method match for `Graph<string>.AddDirectedEdge(GraphNode<string>, GraphNode<string>)' has some invalid arguments
main.cs(131,17): (Location of the symbol related to previous error)
main.cs(212,21): error CS1503: Argument `#1' cannot convert `string' expression to type `GraphNode<string>'
main.cs(213,5): error CS1502: The best overloaded method match for `Graph<string>.AddDirectedEdge(GraphNode<string>, GraphNode<string>)' has some invalid arguments
main.cs(131,17): (Location of the symbol related to previous error)
main.cs(213,21): error CS1503: Argument `#1' cannot convert `string' expression to type `GraphNode<string>'
main.cs(215,5): error CS1502: The best overloaded method match for `Graph<string>.AddDirectedEdge(GraphNode<string>, GraphNode<string>)' has some invalid arguments
main.cs(131,17): (Location of the symbol related to previous error)
main.cs(215,21): error CS1503: Argument `#1' cannot convert `string' expression to type `GraphNode<string>'
main.cs(216,5): error CS1502: The best overloaded method match for `Graph<string>.AddDirectedEdge(GraphNode<string>, GraphNode<string>)' has some invalid arguments
main.cs(131,17): (Location of the symbol related to previous error)
main.cs(216,21): error CS1503: Argument `#1' cannot convert `string' expression to type `GraphNode<string>'
Compilation failed: 22 error(s), 0 warnings

exit status 1

为什么我会收到这些无效参数错误?在 AddDirectedEdge 方法中,我什至注释掉了最后一个参数以及在方法中使用该参数的语句,因此我只能传入两个没有权重的参数,因为这些边应该是未加权的。现在我只是得到了无效的参数错误,我不知道出了什么问题。这是来自 MSDN 网站的完全相同的示例。我在网上的任何地方都找不到答案,所以如果有人能让我知道发生了什么以及如何解决它,我将不胜感激。

最佳答案

你怎么做边缘之间的连接是错误的。您应该连接的不是字符串,而是对象:

首先你应该改变一个方法,添加节点来设置,它应该把添加的节点返回:

public GraphNode<T> AddNode(T value)
{
// adds a node to the graph
var node =new GraphNode<T>(value);
nodeSet.Add(node);
return node;
}

而且你可以连接节点,我只为两个节点制作了一个样本
var privacy = web.AddNode("Privacy.htm");
var people = web.AddNode("People.aspx");

web.AddDirectedEdge(people, privacy); // People -> Privacy

关于c# - msdn C# 图形示例中的无效参数错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/40827844/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com