我的图形类基本上由值Vertices
之间的map
组成,其中每个Vertex
本身就是一个类。每个顶点都有一个值和一个相邻列表,它被实现为相邻的Vertices
和边权重之间的map
。我试图显示图中的每个顶点及其相邻的顶点以及将其连接到其相邻顶点的边的权重。 (对不起,所有的代码)试图显示我的Graph类,有STL映射迭代器的问题
template <class VertexType>
void Graph<VertexType>::display() const
{
typedef map<VertexType, Vertex<VertexType> >::iterator vertices_iter;
typedef map<Vertex<VertexType>, int>::iterator adjList_iter;
for (vertices_iter v_iter = vertices.begin(); v_iter != vertices.end(); v_iter++)
{
cout << "Vertex: " << v_iter->second.value << endl;
cout << setw(25) << left << "Adjacent to: " << "Edge weight:\n";
for (adjList_iter a_iter = vertices[v_iter->first].adjList.begin(); a_iter != vertices[v_iter->first].adjList.end(); a_iter++)
cout << " " << a_iter->first.value << " " << a_iter->second << endl;
cout << endl;
}
}
,但我得到了以下错误:
错误1:
error C2440: 'initializing' : cannot convert from 'std::_Tree_const_iterator<_Mytree>' to 'std::_Tree_iterator<_Mytree>'
1> with
1> [
1> _Mytree=std::_Tree_val<std::_Tmap_traits<unsigned int,Vertex<unsigned int>,std::less<unsigned int>,std::allocator<std::pair<const unsigned int,Vertex<unsigned int>>>,false>>
1> ]
1> No constructor could take the source type, or constructor overload resolution was ambiguous
1> c:\users\user\desktop\cs m20a\topic g project 2\topic g project 2\graph.h(241) : while compiling class template member function 'void Graph<VertexType>::display(void) const'
1> with
1> [
1> VertexType=unsigned int
1> ]
错误2:
error C2678: binary '[' : no operator found which takes a left-hand operand of type 'const std::map<_Kty,_Ty>' (or there is no acceptable conversion)
1> with
1> [
1> _Kty=unsigned int,
1> _Ty=Vertex<unsigned int>
1> ]
1> c:\program files (x86)\microsoft visual studio 10.0\vc\include\map(164): could be 'Vertex<VertexType> &std::map<_Kty,_Ty>::operator [](unsigned int &&)'
1> with
1> [
1> VertexType=unsigned int,
1> _Kty=unsigned int,
1> _Ty=Vertex<unsigned int>
1> ]
1> c:\program files (x86)\microsoft visual studio 10.0\vc\include\map(209): or 'Vertex<VertexType> &std::map<_Kty,_Ty>::operator [](const unsigned int &)'
1> with
1> [
1> VertexType=unsigned int,
1> _Kty=unsigned int,
1> _Ty=Vertex<unsigned int>
1> ]
1> while trying to match the argument list '(const std::map<_Kty,_Ty>, const unsigned int)'
1> with
1> [
1> _Kty=unsigned int,
1> _Ty=Vertex<unsigned int>
1> ]
,这里是我的Graph
类,如果有帮助。
template <class VertexType>
class Graph
{
private:
// list of all vertices in the graph. assumes non-duplicate data.
map< VertexType, Vertex<VertexType> > vertices;
const unsigned MAX_VERTICES; // Maximum number of vertices the graph can hold.
unsigned numVertices; /** Current number of vertices in the graph. */
unsigned numEdges; /** Number of edges in the graph. */
typename map<VertexType, int>::iterator findEdge(const VertexType& v, const VertexType& w) const;
public:
Graph(unsigned max);
unsigned getNumVertices() const;
unsigned getMaxNumVertices() const;
unsigned getNumEdges() const;
int getWeight(const VertexType& v, const VertexType& w) const;
Graph<VertexType>& addVertex(const VertexType& newValue);
Graph<VertexType>& addEdge(const VertexType& v, const VertexType& w, int weight);
void removeEdge(const VertexType& v, const VertexType& w);
void BFS(const VertexType& v) const;
void display() const;
}; // end Graph
那固定的第一个错误,而不是第二。 – JamesGold 2013-05-08 07:52:30
@JamesGold什么原因导致它我不太确定,但你正在做一个额外的查找,你不需要。看我的编辑。 – Yuushi 2013-05-08 08:08:57
这帮助我解决了第二个错误。 – JamesGold 2013-05-08 08:18:08