使用boost图库中的广度优先搜索时遇到问题。 我希望在访问完图表后获取访问者类的属性。
// init my visitor
BfsVisitor bv;
// auto like in C++-0x
BOOST_AUTO (visit, boost::visitor(bv));
// launch search
boost::breadth_first_search (graph, vertex, visit);
// get result ("typedef pair<string, int*> result")
result res = bv.getResult();
// print res: SegFault, `res.second' is NULL
std::cout << *(res.second) << std::endl;
我的BfsVisitor定义如下:
class BfsVisitor : public boost::default_bfs_visitor
{
public:
BfsVisitor()
{
std::cout << boost::this_thread::get_id()
<<" ~~ CONSTRUCTOR" << std::endl;
}
BfsVisitor(const BfsVisitor& b)
: boost::bfs_visitor<>(),
_res (b._res),
_arg (b._arg)
{
std::cout << boost::this_thread::get_id()
<<" ~~ COPY" << std::endl;
}
~BfsVisitor()
{
std::cout << boost::this_thread::get_id()
<<" ~~ DESTRUCTOR" << std::endl;
}
/// Visitor methods
void discover_vertex(const vertex_t& s,
const Graph& g)
{
std::cout << boost::this_thread::get_id()
<< " -> Discover vertex: " << g[s]._id << std::endl;
Executive exec* = g[s]._executive;
_arg.insert(_res);
exec->setArguments(_arg);
// other process
}
void examine_vertex(const vertex_t& s,
const Graph& g)
{
std::cout << boost::this_thread::get_id()
<< " -> Examine vertex: " << g[s]._id << std::endl;
Executive exec* = g[s]._executive;
exec->launch(&_res);
// other process
}
/// Getter
result getResult() const
{
std::cout << boost::this_thread::get_id()
<< " ~~ Get Result" << std::endl;
return _res;
}
private:
result _res;
argument _arg; // typedef std::map
}
我想我的问题来自于访问过程中访问者的组合复制/销毁:这里是一个小图表([1] - &gt; [2] - &gt; [3])的输出示例,只使用一个线程
b7495b40 ~~ COPY
b7495b40 ~~ COPY
b7495b40 ~~ DESTRUCTOR
b7495b40 ~~ COPY
b7495b40 ~~ COPY
b7495b40 ~~ COPY
b7495b40 ~~ COPY
b7495b40 -> Discover vertex: 1
b7495b40 -> Examine vertex: 1
b7495b40 -> Discover vertex: 2
b7495b40 -> Examine vertex: 2
b7495b40 -> Discover vertex: 3
b7495b40 -> Examine vertex: 3
b7495b40 ~~ DESTRUCTOR
b7495b40 ~~ DESTRUCTOR
b7495b40 ~~ DESTRUCTOR
b7495b40 ~~ DESTRUCTOR
b7495b40 ~~ Get Result
Segmentation fault (core dumped)
我只想在examine_vertex方法的所有过程结束时访问我的属性。我检查了boost图访问者文档,但是我找不到任何关于我的问题的解决方案。 希望你有一个想法建议。