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

时间:2016-11-27 10:20:59

标签: c# graph compiler-errors msdn

我正在尝试按照位于此地址的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类来构建此图。在这两个课程之后,我还使用了课程中提供的关于创建图表的代码。这是我尝试在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网站的完全相同的示例。我无法在网上找到答案,所以如果有人能让我知道发生了什么以及如何解决这个问题我会非常感激。

1 个答案:

答案 0 :(得分:1)

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

首先你应该改变一个方法,添加要设置的节点,它应该给添加的节点:

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