2017-04-22 95 views
-1

我想在C++上創建邊緣列表類。 當我編譯代碼時,出現很多錯誤,但我不明白,我錯了什麼?類邊緣列表C++

struct vertex 
{ 
    double x,y; 
}; 

class EdgeList 
{ 
private: 
    std::map<vertex, vector<vertex>> graph_container; 
public: 

    void add_vertex(const vertex& v) { //add a vertex to the map 
     graph_container.insert(pair<vertex,vector<vertex> >(v, vector<vertex>())); 
    } 
    //* 
    void add_edge(const vertex& v, const vertex& u) { //look up vertex in map 
      auto it = graph_container.find(v); 
      if (it != graph_container.end()) 
       *it.push_back(u); 

    }  
    //*/ 
}; 

第一個錯誤是

64-w64-mingw32/include/c++/iostream:39, 
       from EarTr.cpp:1: 
C:/mingw-w64/x86_64-5.2.0-win32-seh-rt_v4-rev0/mingw64/x86_64-w64-mingw32/includ 
e/c++/bits/stl_function.h:387:20: note: 'const vertex' is not derived from 'co 
nst std::__cxx11::basic_string<_CharT, _Traits, _Alloc>' 
     { return __x < __y; } 
+0

投遞問題有關構建錯誤,那麼請包含您獲得實際的錯誤。完整填寫幷包含可能的信息說明。只需將文本(*作爲文本*)複製粘貼到問題主體中即可。然後標記源代碼(最好是[最小,完整和可驗證示例](http://stackoverflow.com/help/mcve)),其中包含錯誤的註釋。 –

+0

但是,您應該首先了解[*運算符優先級*](http://en.cppreference.com/w/cpp/language/operator_precedence)。表達式'* it.push_back(u)'不會做你認爲它做的事。 –

回答

0

我添加了2個功能的結構

bool operator==(const vertex &o)const { 
    return x == o.x && y == o.y; 
} 

bool operator<(const vertex &o) const{ 
    return x < o.x || (x == o.x && y < o.y); 
} 

所以,現在的地圖可以用我喜歡的類型數據的工作。 我也用

it->second.push_back(u); 

,而不是

*it.push_back(u);