【问题标题】:Boost graph library causing errors when applying dijkstra's algorithmBoost 图形库在应用 dijkstra 算法时导致错误
【发布时间】:2014-05-23 04:42:07
【问题描述】:

我一直在松散地关注this examplethis onethis stack overflow post,尝试应用 Dijkstra 算法来找到两个节点之间最短路径的成本。

如果我尝试遵循第一个示例,我会得到一个error with the typedef statement for NameMap。这个错误很神秘,很冗长,我不知道该怎么处理它。

如果我尝试遵循第二个示例(从 Boost 文档复制粘贴!!)它 does not compile。这个错误更加神秘和冗长。

第三个(堆栈溢出帖子)依赖于与第一个相同的 typedef。

这是用户错误吗?可能是这样,但是我应该如何解释从库代码中产生的错误消息?

更新 1

我正在使用来自 debian 测试的 g++ (Debian 4.8.2-21) 4.8.2。

更新 2

Here 是不工作的源代码的压缩版本。有两行以“// 以下行导致错误”开头的行是有问题的。

更新 3 我变了

typedef adjacency_list<listS, vecS, directedS, allow_parallel_edge_tag, EdgeWeightProperty> Graph;

typedef adjacency_list<listS, vecS, directedS, no_property            , EdgeWeightProperty> Graph;

【问题讨论】:

  • 您使用的是哪个编译器/平台?刚刚用 g++ (4.8.2)/linux 尝试了你的第二个例子,它编译没有问题。
  • @jsantander 在我看来像 GCC(编译器错误中的__gnu_cxx)。示例工作正常,问题是他的代码只是“松散地遵循”示例......似乎有点太松散了。
  • g++ (Debian 4.8.2-21) 4.8.2

标签: c++ boost graph dijkstra boost-graph


【解决方案1】:

您的第一次尝试没有使用vertex_name_t 标记定义属性(或将其作为adjacency_list 模板参数传递),因此当您尝试使用该标记创建property_map 时,编译器会发出错误。

您的代码:

typedef property<edge_weight_t, Weight> EdgeWeightProperty;
typedef boost::adjacency_list<listS, vecS, directedS, allow_parallel_edge_tag, EdgeWeightProperty> Graph;
                                                  //  ^ What's this?

你引用的示例代码:

typedef boost::property<boost::edge_weight_t, Weight> WeightProperty;
typedef boost::property<boost::vertex_name_t, std::string> NameProperty;  // <-- not in your code
typedef boost::adjacency_list < boost::listS, boost::vecS, boost::directedS, NameProperty, WeightProperty > Graph;
                                                                         //  ^ Used here

我不知道您为什么将 allow_parallel_edge_tag 作为模板参数传递。如果我正确阅读了文档,那么当您使用自定义容器类型时,该结构是为 parallel_edge_traits 专门设计的。

编辑:一旦你有了代码,第二种情况实际上很容易诊断。通过编译器发出的错误消息,我们寻找编译器没有为dijkstra_shortest_paths 选择三参数重载的原因。很多消息只是告诉您它拒绝了带有大约十几个参数的重载 - 应该如此!

现在,this 错误消息(由 g++ 使用 Coliru 发出)是相关的,因为它告诉您为什么编译器拒绝了三参数版本:

In file included from main.cpp:5:0:
/usr/local/include/boost/graph/dijkstra_shortest_paths.hpp:602:3: note: void boost::
dijkstra_shortest_paths(const VertexListGraph&, typename boost::graph_traits<Graph>::
vertex_descriptor, const boost::bgl_named_params<T, Tag, Base>&) [ /* irrelevant stuff
telling you how it deduced the template parameters here */ ] <near match>
   dijkstra_shortest_paths
   ^
/usr/local/include/boost/graph/dijkstra_shortest_paths.hpp:602:3: note:   no known conversion for
 argument 2 from 'long int [6]' to 'boost::graph_traits<boost::adjacency_list<boost::listS, 
boost::vecS, boost::directedS, boost::no_property, boost::property<boost::edge_weight_t, long int> > 
>::vertex_descriptor {aka long unsigned int}'

你传递了s,包含源顶点的数组,作为指定起始顶点的第二个参数,当你应该传递v0时,编译器正确地抱怨它不能将一个long数组转换为单个顶点。

【讨论】:

  • here 是我的代码的 70 行压缩版本。它应该编译。取消注释任何标有“//以下行导致错误”的行,它应该会燃烧起来。
  • @Sam 第一种情况是相同的:您将 no_property 作为 VertexProperties 模板参数传递,然后尝试在 NameMap 中使用 vertex_name_t(您实际上并没有使用)。我已经用你的第二种情况更新了我的答案。
  • 谢谢。现在它编译了。我会详细了解这个神秘的NameProperty 值。
猜你喜欢
  • 1970-01-01
  • 2012-07-28
  • 1970-01-01
  • 2018-06-03
  • 2013-10-13
  • 2010-11-23
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多