2016-11-27 19 views
1

我想在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。我已经使用二叉树课程中的Node类和NodeList类来构建此图。在这两个课程之后,我还使用了本课程中关于创建图形的代码。这是我的代码,我试图在https://repl.it/languages/csharp运行:MSDN中的参数无效错误C#图示例

`

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网站的例子完全相同。我无法在网上找到任何答案,所以如果有人能让我知道发生了什么事以及如何修复它,我将不胜感激。

回答

1

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

首先,你应该改变的方法,增加节点设置,它应该给theadded节点回:

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 
+0

非常感谢您的回答和解释。它现在按预期工作。 – SineLaboreNihil

+0

@SineLaboreNihil欢迎:) –