I am writing a class for working with undirected graphs, and encountered the following compile-time errors:
The best overloaded method match for
‘Dictionary.EdgeCollection>.Add(TVertex, UndirectedGraph.EdgeCollection)’
has some invalid argumentsArgument 2: cannot convert
fromUndirectedGraph<TVertex,TEdge>.AdjacentEdgeCollection<TVertex,TEdge>
toUndirectedGraph<TVertex,TEdge>.AdjacentEdgeCollection<TVertex,TEdge>
I could reduce the problem to the following example:
public class UndirectedGraph<TVertex, TEdge>
{
Dictionary<TVertex, EdgeCollection<TVertex, TEdge>> edges;
class VertexCollection<TVertex, TEdge>
{
UndirectedGraph<TVertex, TEdge> graph;
public VertexCollection(UndirectedGraph<TVertex, TEdge> graph)
{ this.graph = graph; }
public void Add(TVertex value)
{
// Argument 2: cannot convert
// from 'UndirectedGraph<TVertex,TEdge>.AdjacentEdgeCollection<TVertex,TEdge>'
// to 'UndirectedGraph<TVertex,TEdge>.AdjacentEdgeCollection<TVertex,TEdge>'
this.graph.edges.Add(value, new EdgeCollection<TVertex, TEdge>(this.graph));
}
}
class EdgeCollection<TVertex, TEdge>
{
public EdgeCollection(UndirectedGraph<TVertex, TEdge> graph) { }
}
}
Note that TVertex and TEdge in the nested classes are different from TVertex and TEdge in the outer class, and I get warnings stating that I should rename them. I can do that, but this does not affect the errors. I think the purpose of the fragment is clear, so how do I get it to do what I want and where does my thinking go wrong?
Are you sure that there are three
TVertextype parameters and threeTEdgetype parameters? It seems to me that that all three are same and what you need is the following: