我正在尝试使用associative_property_map来包含顶点的索引,但是我使用以下简单代码得到以下错误,问题是什么?
#include <boost/graph/iteration_macros.hpp>
#include <boost/graph/adjacency_list.hpp>
using namespace std;
using namespace boost;
struct NodeData
{
int label;
};
struct EdgeData
{
int age;
};
typedef map<vecS, size_t> IndexMap;
IndexMap mapIndex;
associative_property_map<IndexMap> propmapIndex(mapIndex);
typedef adjacency_list<setS, setS, undirectedS, NodeData, EdgeData> Graph;
typedef Graph::vertex_descriptor NodeID;
typedef Graph::edge_descriptor EdgeID;
int main()
{
Graph g;
NodeID n0 = add_vertex(g); g[n0].label = -1;
NodeID n1 = add_vertex(g); g[n1].label = -1;
EdgeID edge; bool ok;
tie(edge, ok) = boost::add_edge(n0, n1, g);
if (ok) g[edge].age = 10;
int i=0;
BGL_FORALL_VERTICES(v, g, Graph)
{
put(propmapIndex, v, i++);
}
return 0;
}
错误:
c:\program files\codeblocks\mingw\bin..\lib\gcc\mingw32\4.4.1........\include\boost\property_map\property_map.hpp||In
function'void boost :: put(const boost :: put_get_helper&amp;,K,const V&amp;) [使用PropertyMap = boost :: associative_property_map,std :: allocator&gt;
,Reference = unsigned int&amp;,K = void *,V = int]':| C:\ Users \ memo \ Desktop \ Debuged \ boostGraph \ main.cpp | 39 |实例化 这里| C:\程序 文件\代码块\ MinGW的\ BIN .. \ LIB \ GCC \的mingw32 \ 4.4.1 ........ \ \包括升压\ property_map \ property_map.hpp | 361 |错误: 在'(const boost :: associative_property_map中,'operator []'不匹配 std :: allocator&gt; &GT; &gt;&amp;)((const boost :: associative_property_map, std :: allocator&gt; &GT; &gt; *)(&amp; pa))[k]'| C:\程序 文件\代码块\ MinGW的\ BIN .. \ LIB \ GCC \的mingw32 \ 4.4.1 ........ \ \包括升压\ property_map \ property_map.hpp | 498 |注: 候选人是:typename UniquePairAssociativeContainer :: VALUE_TYPE :: second_type&安培; boost :: associative_property_map :: operator [](const typename UniquePairAssociativeContainer :: key_type&amp;)const [with UniquePairAssociativeContainer = std :: map,std :: allocator&gt; &GT;] | || === 构建完成:1个错误,0个警告=== |
由于
答案 0 :(得分:5)
顶点描述符必须指定给IndexMap,因此它是map<NodeID, size_t>
而不是map<vecS, size_t>
:
<...>
typedef Graph::vertex_descriptor NodeID;
typedef map<NodeID, size_t> IndexMap;
IndexMap mapIndex;
associative_property_map<IndexMap> propmapIndex(mapIndex);
<...>
// indexing all vertices
int i=0;
BGL_FORALL_VERTICES(v, g, Graph)
{
put(propmapIndex, v, i++);
}