2013-12-19 47 views
0

我試圖實現使用鏈表數組相鄰列表:目前需要幫助找出什麼語法工作與我的數據結構

vector< list<Edge> > adjList; 

,我無法得到它的編譯,因爲我不是相當確定如何訪問頂點或甚至我的嵌套類Edge的權重

頂點。這是錯誤的輸出...

Graph.cpp: In member function `void Graph::set_Edge(std::string, std::string, int)': 
Graph.cpp:30: error: 'class std::list<Graph::Edge, std::allocator<Graph::Edge> >' has no member named 'm_vertex' 
makefile.txt:9: recipe for target `Graph.o' failed 
make: *** [Graph.o] Error 1 

這裏是文件Graph.h(在類聲明有關的所有評論對不起,我喜歡把所有的留在我的代碼我的想法,直到我已經準備好了把它的...)

#ifndef GRAPH_H_INCLUDED 
#define GRAPH_H_INCLUDED 
//class MinPriority; 
#include <iostream> 
#include <string> 
#include <vector> 
#include <list> 
using namespace std; 
class Graph 
{ 
public: 
    Graph(); 
    ~Graph(); 
    /*void setArray(string vertex); 
    void sortArray(); 
    Graph* getGraph(string preVertex, string vertex, int weight); 
    void setGraph(string vertex, string postVertex, int weight);*/ 
    void set_Edge(string targetVertex, string vertex, int weight); 
    friend class MinPriority; 
private: 
    class Edge 
    { 
    public: 
     Edge(string vertex, int weight) 
     {m_vertex = vertex; m_weight = weight;} 
     ~Edge(); 
     string get_vertex(){} 
     int get_weight(){} 
    private: 
     string m_vertex; 
     int m_weight; 
    }; 
    vector< list<Edge> > adjList; 

}; 


#endif // GRAPH_H_INCLUDED 

這裏是Graph.cpp

#include "Graph.h" 

void Graph::set_Edge(string targetVertex, string vertex, int weight) //find target vertex 
{ 
    for(unsigned int u = 0; u <= adjList.size(); u++)//traverses through the Y coord of the 2D array 
    { 
     if(targetVertex == adjList[u].m_vertex) // <<THIS IS WHERE THE ERROR OCCURS!!!! 
     { 
      adjList[u].push_back(Edge(vertex, weight)); //push new element to the back of the linked list at array index u. 
     } 

    } 
    //we now need to add 
    //adjList[adjList.size()].push_back(Edge(vertex, weight)); 
} 

同樣,我基本上需要幫助找出如何訪問邊緣(或頂點或重量)的部分if(targetVertex == adjList[u].m_vertex)是我希望使用它認爲它會在數組中找到'u'索引並檢查'u'索引的頂點元素並將其與目標頂點進行比較。

+1

你的載體包含列表。你正在循環向量的元素。所以你迭代的每個元素都是...列表!不是邊緣或其他任何東西。 – juanchopanza

回答

1

adjList的類型爲list<Edge>,所以它沒有任何成員名爲m_vertex。它包含的節點有m_vertex成員。

#include "Graph.h" 

void Graph::set_Edge(string targetVertex, string vertex, int weight) 
{ 
    for(unsigned int u = 0; u <= adjList.size(); u++) 
    { 
     if(adjList[u].front().m_vertex == targetVertex) 
        //^^add front() to access a list node 
        //because the m_vertex member is same of all the edges in the list 
        //So just access the first one for simplicity. 
     { 
      adjList[u].push_back(Edge(vertex, weight)); 
     } 
    } 
}