在boost图库中为深度优先搜索提供颜色图时遇到问题



我正在尝试从特定顶点开始使用深度优先搜索。为此,我需要提供一个访客和一张彩色地图。

如果我不提供起始顶点,那么我也不需要提供颜色贴图,一切都很好。然而,我没有找到一个可以接受起始顶点而不需要颜色贴图的签名。

我使用C++17(Embarcadero C++Builder(这是代码:

#include <stdio.h>
#include <boost/graph/graph_traits.hpp>
#include <boost/graph/adjacency_list.hpp>
#include <boost/graph/breadth_first_search.hpp>
#include <boost/graph/depth_first_search.hpp>
#include <boost/graph/topological_sort.hpp>

struct EdgeProps
{
int value;
};

struct VertexProps
{
int value;
};

struct cycle_detector : public boost::default_dfs_visitor
{
cycle_detector( bool& has_cycle)
: _has_cycle(has_cycle) { }

template <class Edge, class Graph>
void back_edge(Edge, Graph&) {
_has_cycle = true;
}
protected:
bool& _has_cycle;
};

int _tmain(int argc, _TCHAR* argv[]) 
{
typedef boost::adjacency_list<boost::setS, boost::vecS, boost::bidirectionalS, VertexProps, EdgeProps> Graph;
Graph g;
auto v0 = boost::add_vertex(g);
auto v1 = boost::add_vertex(g);
auto v2 = boost::add_vertex(g);
auto v3 = boost::add_vertex(g);
auto v4 = boost::add_vertex(g);

boost::add_edge(v0, v1, g);
boost::add_edge(v0, v2, g);
boost::add_edge(v0, v3, g);
boost::add_edge(v2, v4, g);
boost::add_edge(v3, v4, g);

bool has_cycle = false;

std::vector<boost::default_color_type> colors(boost::num_vertices(g));
boost::iterator_property_map color_map(colors.begin(), boost::get(boost::vertex_index, g));

boost::depth_first_search(g, boost::visitor(cycle_detector(has_cycle)), color_map, v0);

return 0;
}

我收到一大堆抱怨来访者的错误信息。然而,如果我不提供一个地图和一个起始顶点,那么一切都很好。

以下是信息:

bcc32c command line for "DFS.cpp"
c:program files (x86)embarcaderostudio21.0binbcc32c.exe -cc1 -D _DEBUG -output-dir .Win32Debug -I D:StudyTrees -I D:StudyTrees -isystem 
"c:program files (x86)embarcaderostudio21.0include" -isystem "c:program files (x86)embarcaderostudio21.0includedinkumware64" -isystem 
"c:program files (x86)embarcaderostudio21.0includewindowscrtl" -isystem "c:program files (x86)embarcaderostudio21.0includewindowssdk" 
-isystem "c:program files (x86)embarcaderostudio21.0includewindowsrtl" -isystem "c:program files 
(x86)embarcaderostudio21.0includewindowsvcl" -isystem "c:program files (x86)embarcaderostudio21.0includewindowsfmx" -isystem 
C:UsersPublicDocumentsEmbarcaderoStudio21.0hppWin32 -isystem "c:program files (x86)embarcaderostudio21.0includeboost_1_70" -isystem 
"D:IcrfsComponentsTeeChart 2020.30CompiledDelphi27.win32Include" -isystem "D:ICRFSComponentsOrpheus SydneyWin32hpp" -isystem "c:program 
files (x86)embarcaderostudio21.0includeboost_1_70\BOOST" -isystem D:ICRFSComponentsSimpleGraphhpp -isystem 
C:UsersPublicDocumentsEmbarcaderoStudio21.0hppWin32 -debug-info-kind=standalone -fborland-extensions -nobuiltininc -nostdsysteminc -triple 
i686-pc-windows-omf -emit-obj -mrelocation-model static -masm-verbose -ffunction-sections -fexceptions -fcxx-exceptions -fseh -mstack-alignment=16 
-fno-spell-checking -fno-use-cxa-atexit -fno-threadsafe-statics -main-file-name DFS.cpp -x c++ -std=c++17 -O0 -fmath-errno -tC -tM -o 
.Win32DebugDFS.obj --auto-dependency-output -MT .Win32DebugDFS.obj -include-pch .Win32DebugTreesPCH1.pch DFS.cpp 
[bcc32c Error] depth_first_search.hpp(234): no member named 'initialize_vertex' in 'boost::bgl_named_params<cycle_detector, boost::graph_visitor_t, boost::no_property>'
DFS.cpp(62): in instantiation of function template specialization 'boost::depth_first_search<boost::adjacency_list<boost::setS, boost::vecS, boost::bidirectionalS, VertexProps, EdgeProps, boost::no_property, boost::listS>, boost::bgl_named_params<cycle_detector, boost::graph_visitor_t, boost::no_property>, boost::iterator_property_map<std::_Vector_iterator<std::_Vector_val<std::_Simple_types<boost::default_color_type> > >, boost::vec_adj_list_vertex_id_map<VertexProps, unsigned int>, boost::default_color_type, boost::default_color_type &> >' requested here
[bcc32c Error] depth_first_search.hpp(237): no member named 'start_vertex' in 'boost::bgl_named_params<cycle_detector, boost::graph_visitor_t, boost::no_property>'
[bcc32c Error] depth_first_search.hpp(245): no member named 'start_vertex' in 'boost::bgl_named_params<cycle_detector, boost::graph_visitor_t, boost::no_property>'
[bcc32c Error] depth_first_search.hpp(39): no member named 'initialize_vertex' in 'boost::bgl_named_params<cycle_detector, boost::graph_visitor_t, boost::no_property>'
general.hpp(47): in instantiation of member function 'boost::DFSVisitorConcept<boost::bgl_named_params<cycle_detector, boost::graph_visitor_t, boost::no_property>, boost::adjacency_list<boost::setS, boost::vecS, boost::bidirectionalS, VertexProps, EdgeProps, boost::no_property, boost::listS> >::constraints' requested here
depth_first_search.hpp(227): in instantiation of member function 'boost::concepts::constraint<boost::DFSVisitorConcept<boost::bgl_named_params<cycle_detector, boost::graph_visitor_t, boost::no_property>, boost::adjacency_list<boost::setS, boost::vecS, boost::bidirectionalS, VertexProps, EdgeProps, boost::no_property, boost::listS> > >::failed' requested here
DFS.cpp(62): in instantiation of function template specialization 'boost::depth_first_search<boost::adjacency_list<boost::setS, boost::vecS, boost::bidirectionalS, VertexProps, EdgeProps, boost::no_property, boost::listS>, boost::bgl_named_params<cycle_detector, boost::graph_visitor_t, boost::no_property>, boost::iterator_property_map<std::_Vector_iterator<std::_Vector_val<std::_Simple_types<boost::default_color_type> > >, boost::vec_adj_list_vertex_id_map<VertexProps, unsigned int>, boost::default_color_type, boost::default_color_type &> >' requested here
[bcc32c Error] depth_first_search.hpp(40): no member named 'start_vertex' in 'boost::bgl_named_params<cycle_detector, boost::graph_visitor_t, boost::no_property>'
[bcc32c Error] depth_first_search.hpp(41): no member named 'discover_vertex' in 'boost::bgl_named_params<cycle_detector, boost::graph_visitor_t, boost::no_property>'
[bcc32c Error] depth_first_search.hpp(42): no member named 'examine_edge' in 'boost::bgl_named_params<cycle_detector, boost::graph_visitor_t, boost::no_property>'
[bcc32c Error] depth_first_search.hpp(43): no member named 'tree_edge' in 'boost::bgl_named_params<cycle_detector, boost::graph_visitor_t, boost::no_property>'
[bcc32c Error] depth_first_search.hpp(44): no member named 'back_edge' in 'boost::bgl_named_params<cycle_detector, boost::graph_visitor_t, boost::no_property>'
[bcc32c Error] depth_first_search.hpp(45): no member named 'forward_or_cross_edge' in 'boost::bgl_named_params<cycle_detector, boost::graph_visitor_t, boost::no_property>'
[bcc32c Error] depth_first_search.hpp(47): no member named 'finish_vertex' in 'boost::bgl_named_params<cycle_detector, boost::graph_visitor_t, boost::no_property>'
[bcc32c Error] depth_first_search.hpp(134): no member named 'discover_vertex' in 'boost::bgl_named_params<cycle_detector, boost::graph_visitor_t, boost::no_property>'
depth_first_search.hpp(238): in instantiation of function template specialization 'boost::detail::depth_first_visit_impl<boost::adjacency_list<boost::setS, boost::vecS, boost::bidirectionalS, VertexProps, EdgeProps, boost::no_property, boost::listS>, boost::bgl_named_params<cycle_detector, boost::graph_visitor_t, boost::no_property>, boost::iterator_property_map<std::_Vector_iterator<std::_Vector_val<std::_Simple_types<boost::default_color_type> > >, boost::vec_adj_list_vertex_id_map<VertexProps, unsigned int>, boost::default_color_type, boost::default_color_type &>, boost::detail::nontruth2>' requested here
DFS.cpp(62): in instantiation of function template specialization 'boost::depth_first_search<boost::adjacency_list<boost::setS, boost::vecS, boost::bidirectionalS, VertexProps, EdgeProps, boost::no_property, boost::listS>, boost::bgl_named_params<cycle_detector, boost::graph_visitor_t, boost::no_property>, boost::iterator_property_map<std::_Vector_iterator<std::_Vector_val<std::_Simple_types<boost::default_color_type> > >, boost::vec_adj_list_vertex_id_map<VertexProps, unsigned int>, boost::default_color_type, boost::default_color_type &> >' requested here
[bcc32c Error] depth_first_search.hpp(155): no member named 'examine_edge' in 'boost::bgl_named_params<cycle_detector, boost::graph_visitor_t, boost::no_property>'
[bcc32c Error] depth_first_search.hpp(158): no member named 'tree_edge' in 'boost::bgl_named_params<cycle_detector, boost::graph_visitor_t, boost::no_property>'
[bcc32c Error] depth_first_search.hpp(163): no member named 'discover_vertex' in 'boost::bgl_named_params<cycle_detector, boost::graph_visitor_t, boost::no_property>'
[bcc32c Error] depth_first_search.hpp(179): no member named 'finish_vertex' in 'boost::bgl_named_params<cycle_detector, boost::graph_visitor_t, boost::no_property>'
Failed
Elapsed time: 00:00:02.8

如有任何帮助,将不胜感激

将访问者包装在boost::visitor中会混淆命名参数重载。只需通过访客:

boost::depth_first_search(g, cycle_detector{has_cycle}, color_map, v0);

将所有参数作为命名参数传递:

boost::depth_first_search(g,
boost::visitor(cycle_detector{has_cycle})
.color_map(color_map)
.root_vertex(v0));

在Coliru上直播

#include <boost/graph/adjacency_list.hpp>
#include <boost/graph/breadth_first_search.hpp>
#include <boost/graph/depth_first_search.hpp>
#include <boost/graph/graph_traits.hpp>
#include <boost/graph/topological_sort.hpp>
#include <stdio.h>
#ifndef IHAVEASANECOMPILER_PLATFORM
#define _tmain main
#define _TCHAR char
#endif
struct EdgeProps {
int value;
};
struct VertexProps {
int value;
};
struct cycle_detector : boost::default_dfs_visitor {
cycle_detector(bool& has_cycle) : _has_cycle(has_cycle) {}
template <class Edge, class Graph> void back_edge(Edge, Graph&) {
_has_cycle = true;
}
bool& _has_cycle;
};
int _tmain() {
typedef boost::adjacency_list<boost::setS, boost::vecS,
boost::bidirectionalS, VertexProps, EdgeProps>
Graph;
Graph g;
auto v0 = boost::add_vertex(g);
auto v1 = boost::add_vertex(g);
auto v2 = boost::add_vertex(g);
auto v3 = boost::add_vertex(g);
auto v4 = boost::add_vertex(g);
boost::add_edge(v0, v1, g);
boost::add_edge(v0, v2, g);
boost::add_edge(v0, v3, g);
boost::add_edge(v2, v4, g);
boost::add_edge(v3, v4, g);
bool has_cycle = false;
std::vector<boost::default_color_type> colors(boost::num_vertices(g));
boost::iterator_property_map color_map(colors.begin(), boost::get(boost::vertex_index, g));
boost::depth_first_search(g, cycle_detector{has_cycle}, color_map, v0);
}

相关内容

最新更新