c++ - BGL depth_first_search 颜色图错误

标签 c++ boost

对于新手问题,我深表歉意,但我正在竭尽全力尝试开始使用 BGL。我正在尝试深度优先搜索:

boost::default_dfs_visitor vis;
boost::depth_first_search( graph, boost::visitor(vis) );

我收到了大量关于彩色 map 的错误消息:

In file included from /usr/include/boost/graph/named_function_params.hpp:22,
                 from /usr/include/boost/graph/depth_first_search.hpp:21,
                 from main.cc:6:
/usr/include/boost/property_map/shared_array_property_map.hpp: In member function 'T& boost::shared_array_property_map<T, IndexMap>::operator[](typename boost::property_traits<IndexMap>::key_type) const [with T = boost::default_color_type, IndexMap = boost::adj_list_vertex_property_map<boost::adjacency_list<boost::vecS, boost::listS, boost::directedS, boost::property<boost::vertex_treegraphnode_t, TreeGraph::node, boost::no_property>, boost::no_property, boost::no_property, boost::listS>, boost::detail::error_property_not_found, const boost::detail::error_property_not_found&, boost::vertex_index_t>]':
/usr/include/boost/property_map/property_map.hpp:324:   instantiated from 'void boost::put(const boost::put_get_helper<Reference, PropertyMap>&, K, const V&) [with PropertyMap = boost::shared_array_property_map<boost::default_color_type, boost::adj_list_vertex_property_map<boost::adjacency_list<boost::vecS, boost::listS, boost::directedS, boost::property<boost::vertex_treegraphnode_t, TreeGraph::node, boost::no_property>, boost::no_property, boost::no_property, boost::listS>, boost::detail::error_property_not_found, const boost::detail::error_property_not_found&, boost::vertex_index_t> >, Reference = boost::default_color_type&, K = void*, V = boost::default_color_type]'
/usr/include/boost/graph/depth_first_search.hpp:196:   instantiated from 'void boost::depth_first_search(const VertexListGraph&, DFSVisitor, ColorMap, typename boost::graph_traits<Graph>::vertex_descriptor) [with VertexListGraph = boost::adjacency_list<boost::vecS, boost::listS, boost::directedS, boost::property<boost::vertex_treegraphnode_t, TreeGraph::node, boost::no_property>, boost::no_property, boost::no_property, boost::listS>, DFSVisitor = boost::dfs_visitor<boost::null_visitor>, ColorMap = boost::shared_array_property_map<boost::default_color_type, boost::adj_list_vertex_property_map<boost::adjacency_list<boost::vecS, boost::listS, boost::directedS, boost::property<boost::vertex_treegraphnode_t, TreeGraph::node, boost::no_property>, boost::no_property, boost::no_property, boost::listS>, boost::detail::error_property_not_found, const boost::detail::error_property_not_found&, boost::vertex_index_t> >]'
/usr/include/boost/graph/depth_first_search.hpp:295:   instantiated from 'void boost::depth_first_search(const VertexListGraph&, const boost::bgl_named_params<P, T, R>&) [with VertexListGraph = TreeGraph::graph_type, P = boost::dfs_visitor<boost::null_visitor>, T = boost::graph_visitor_t, R = boost::no_property]'
main.cc:272:   instantiated from here
/usr/include/boost/property_map/shared_array_property_map.hpp:36: error: no match for 'operator[]' in '((const boost::shared_array_property_map<boost::default_color_type, boost::adj_list_vertex_property_map<boost::adjacency_list<boost::vecS, boost::listS, boost::directedS, boost::property<boost::vertex_treegraphnode_t, TreeGraph::node, boost::no_property>, boost::no_property, boost::no_property, boost::listS>, boost::detail::error_property_not_found, const boost::detail::error_property_not_found&, boost::vertex_index_t> >*)this)->boost::shared_array_property_map<boost::default_color_type, boost::adj_list_vertex_property_map<boost::adjacency_list<boost::vecS, boost::listS, boost::directedS, boost::property<boost::vertex_treegraphnode_t, TreeGraph::node, boost::no_property>, boost::no_property, boost::no_property, boost::listS>, boost::detail::error_property_not_found, const boost::detail::error_property_not_found&, boost::vertex_index_t> >::data[boost::get [with PropertyMap = boost::adj_list_vertex_property_map<boost::adjacency_list<boost::vecS, boost::listS, boost::directedS, boost::property<boost::vertex_treegraphnode_t, TreeGraph::node, boost::no_property>, boost::no_property, boost::no_property, boost::listS>, boost::detail::error_property_not_found, const boost::detail::error_property_not_found&, boost::vertex_index_t>, Reference = const boost::detail::error_property_not_found&, K = void*](((const boost::put_get_helper<const boost::detail::error_property_not_found&, boost::adj_list_vertex_property_map<boost::adjacency_list<boost::vecS, boost::listS, boost::directedS, boost::property<boost::vertex_treegraphnode_t, TreeGraph::node, boost::no_property>, boost::no_property, boost::no_property, boost::listS>, boost::detail::error_property_not_found, const boost::detail::error_property_not_found&, boost::vertex_index_t> >&)((const boost::put_get_helper<const boost::detail::error_property_not_found&, boost::adj_list_vertex_property_map<boost::adjacency_list<boost::vecS, boost::listS, boost::directedS, boost::property<boost::vertex_treegraphnode_t, TreeGraph::node, boost::no_property>, boost::no_property, boost::no_property, boost::listS>, boost::detail::error_property_not_found, const boost::detail::error_property_not_found&, boost::vertex_index_t> >*)(&((const boost::shared_array_property_map<boost::default_color_type, boost::adj_list_vertex_property_map<boost::adjacency_list<boost::vecS, boost::listS, boost::directedS, boost::property<boost::vertex_treegraphnode_t, TreeGraph::node, boost::no_property>, boost::no_property, boost::no_property, boost::listS>, boost::detail::error_property_not_found, const boost::detail::error_property_not_found&, boost::vertex_index_t> >*)this)->boost::shared_array_property_map<boost::default_color_type, boost::adj_list_vertex_property_map<boost::adjacency_list<boost::vecS, boost::listS, boost::directedS, boost::property<boost::vertex_treegraphnode_t, TreeGraph::node, boost::no_property>, boost::no_property, boost::no_property, boost::listS>, boost::detail::error_property_not_found, const boost::detail::error_property_not_found&, boost::vertex_index_t> >::index))), ((void* const&)((void* const*)(& v))))]'
/usr/include/boost/smart_ptr/shared_array.hpp:85: note: candidates are: T& boost::shared_array<T>::operator[](ptrdiff_t) const [with T = boost::default_color_type]

depth_first_search 的文档说有一个默认值,而且我见过很多不提供自己的颜色图的示例,所以我不确定为什么会出现问题。

我的图表类型是:

typedef
  boost::adjacency_list<
  boost::vecS, boost::listS, boost::directedS,
  boost::property<boost::vertex_treegraphnode_t, TreeGraph::node>
  >
  graph_type;

我想也许因为我有一个自定义属性映射,也许它会覆盖 depth_first_search 需要的一些颜色映射默认值,但我补充说:

boost::property<boost::vertex_color_t, boost::default_color_type>

到属性列表,这也没有帮助。

我尝试使用外部 color_map 进行深度优先搜索,但似乎也无济于事。

关于我可能做错了什么有什么想法吗?

谢谢。

最佳答案

const boost::detail::error_property_not_found&, boost::vertex_index_t

这是“错误之海”的重要部分。只有将 vecS 作为 VertexList 模板参数的 adjacency_list 具有默认的内部 vertex_index 属性,并且此属性由默认颜色图(您正在使用 listS)。您有多种选择:

  • 您可以简单地使用 vecS 作为第二个参数(如果您不需要动态修改图形,这是最简单的方法)。
  • 您可以创建一个内部属性,然后确保对其进行初始化并使其与图表保持同步。为此,您需要将 VertexProperty 定义为:

    boost::property<vertex_index_t, std::size_t, boost::property<boost::vertex_treegraphnode_t, TreeGraph::node>>
    

    然后:

    boost::property_map<graph_type,vertex_index_t>::type my_vertex_index_map=get(vertex_index,graph);
    std::size_t current_index=0;
    BGL_FORALL_VERTICES(v,graph,graph_type)
        put(my_vertex_index_map,v,current_index++);
    
  • 您可以创建一个外部属性,对其进行初始化并保持最新,然后使用命名参数将其传递给 depth_first_search

    typedef graph_traits<graph_type>::vertex_descriptor VertexDesc;
    typedef map<VertexDesc, size_t> VertexIndexMap;
    VertexIndexMap mapIndex;
    boost::associative_property_map<VertexIndexMap> my_vertex_index_map(mapIndex);
    std::size_t current_index=0;
    BGL_FORALL_VERTICES(v,graph,graph_type)
        put(my_vertex_index_map,v,current_index++);
    ...
    boost::depth_first_search( graph, boost::visitor(vis).vertex_index_map(my_vertex_index_map);
    

关于c++ - BGL depth_first_search 颜色图错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/15649166/

相关文章:

c++ - 使用 ostream 重载 << 运算符不起作用。为什么?

operator new 的 C++ 对齐,有多大关系?

c++ - 如果传递函数,右值引用是否优化?

c++ - 在 C++ 中读取文件并处理可能的错误的可移植方法

c++使用boost/math进行连续分数评估

c++ - 从网络队列中读取多个数据报

c++ - 函数调用守卫

c++ - 如何从 std::vector 或列表中选择一个子集?

c++ - #include <boost/chrono.hpp> 导致无法解析的外部符号,使用了 bcp

c++ - 编译 Liblas