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

Invalid arguments error in msdn C# graph example

我正在尝试按照位于以下地址的 Microsoft Developer Network 上的教程在 C# 中构建图形数据结构:https://msdn.microsoft.com/en-us/library/ms379574(v=vs.80).aspx. The tutorial continues from a previous lesson on binary trees located here: https://msdn.microsoft.com/en-us/library/ms379572(v=vs.80).aspx. I have used the Node class and the NodeList class from the binary trees lesson to build this graph. Following those two classes I have also used the code presented in the lesson on creating graphs. This is my code that I'm trying to run on 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