2016-11-27 6 views
1

このアドレスにある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。このグラフを作成するには、NodeクラスとNodeListクラスをバイナリツリーレッスンから使用しました。これらの2つのクラスに続いて、グラフの作成に関するレッスンのコードも使用しました。これは私が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メソッドでは、最後のパラメータとメソッドでそのパラメータを使用しているステートメントをコメントアウトしているので、重みがなくても2つの引数だけを渡すことができるため、これらのエッジは重み付けされていないためです。今私は無効な引数のエラーを取得し、私は何が間違っているか把握することはできません。これは、MSDNのWebサイトとまったく同じ例です。私はこれにオンラインで答えを見つけることができないので、誰かが私に何が起こっているのかを教えてもらえれば、それを修正することができます。本当に感謝します。

答えて

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

を、あなたはノードを接続できるよりも、私が作りました2ノードのみのサンプル

var privacy = web.AddNode("Privacy.htm"); 
var people = web.AddNode("People.aspx"); 

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

ありがとう、あなたの答えと説明をありがとう。今は期待どおりに動作します。 – SineLaboreNihil

+0

@SineLaboreNihilあなたは大歓迎です:) –