2013-05-08 30 views
1

我的圖形類基本上由值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 

回答

1

你聲明方法const,也就是說,它不會改變任何一個成員變量:

void Graph<VertexType>::display() const 

這意味着你必須只使用const底層成員變量的方法;因此const_iterator爲了你們的基本類型:

typedef map<VertexType, Vertex<VertexType> >::const_iterator vertices_iter; 
typedef map<Vertex<VertexType>, int>::const_iterator adjList_iter; 

更重要的是,如果你使用C++11,利用auto

至於你第二個錯誤,我無法看到究竟是什麼原因造成的,但是,你正在做的工作,查找無故:

for(auto a_iter = vertices[v_iter->first].adjList.begin(); 
    a_iter != vertices[v_iter->first].adjList.end(); 
    a_iter++) 

v_iter是在原環vertices.begin()。然後vertices[v_iter->first]*v.begin()實際上是一樣的。你可以取代這個:

for(auto a_iter = v_iter->first.adjList.begin(); 
    a_iter != v_iter->first.adjList.end(); 
    a_iter++) 
+0

那固定的第一個錯誤,而不是第二。 – JamesGold 2013-05-08 07:52:30

+0

@JamesGold什麼原因導致它我不太確定,但你正在做一個額外的查找,你不需要。看我的編輯。 – Yuushi 2013-05-08 08:08:57

+0

這幫助我解決了第二個錯誤。 – JamesGold 2013-05-08 08:18:08

1

的問題是,

void Graph<VertexType>::display() const 

是一個const方法,所以圖中的每個成員也const的在裏面。

在這裏,您使用非常量迭代

for (vertices_iter v_iter = vertices.begin(); v_iter != vertices.end(); v_iter++) 

嘗試常量迭代器,而不是

typedef map<VertexType, Vertex<VertexType> >::const_iterator vertices_iter; 
+0

感謝您的回答。這解決了第一個錯誤,但不是第二個錯誤。 – JamesGold 2013-05-08 08:01:35