2

我在使用 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] -> [2] -> [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)

我只想在检查_vertex 方法中执行的所有过程结束时访问我的属性。我检查了 boost graph visitor 文档,但没有找到任何关于我的问题的解决方案。希望你有想法可以提出建议。

4

0 回答 0