【问题标题】:Boost Djikstra class "boost::property_map> has no member "type"Boost Dijkstra 类 "boost::property map> 没有成员 "type"
【发布时间】:2018-08-30 14:32:25
【问题描述】:

一段时间以来,我一直在努力使用 Boost 框架实现 Djikstra 算法,但我似乎无法弄清楚我缺少什么。

使用在以下位置找到的示例:https://www.boost.org/doc/libs/1_67_0/libs/graph/doc/dijkstra_shortest_paths.html

第 36 行出现错误

property_map<graph_t, edge_weight_t>::type weightmap = get(edge_weight, g);

class "boost::property_map<boost::adjacency_list<boost::listS, boost::vecS, boost::directedS, boost::no_property, boost::property<boost::edge_weight_t, int, boost::no_property>, boost::no_property, boost::listS>, boost::edge_weight_t, void>" has no member "type"

我找不到其他人有同样的问题,而且我似乎无法找出问题所在。对此问题的任何帮助将不胜感激。

提前致谢

【问题讨论】:

  • 好吧,这个错误准确地告诉你出了什么问题。在property_map 类中没有任何东西叫type。你到底想从那里获得什么?
  • 你的 Boost 版本是否与示例中的版本相同(1.67.0)?
  • 是的,我使用的是 1.67.0。我知道property_map 类中没有任何称为 type 的东西,但是我见过的所有 djikstra 示例(无论哪个版本)似乎都使用了这个 ::type 成员,我似乎找不到。如果我错过了什么,我就无法解决。
  • 试试 property_map weightmap = get(edge_weight, g);

标签: c++ boost graph


【解决方案1】:

问题可能是您未能在图上定义边权重属性。例如:

Live On Coliru

using graph_t = boost::adjacency_list</*boost::vecS, boost::vecS, boost::directedS, boost::no_property, boost::no_property*/>;

boost::property_map<graph_t, boost::edge_weight_t>::type weightmap = boost::get(boost::edge_weight, g);

不编译。您应该添加一个:

Live On Coliru

#include <boost/graph/adjacency_list.hpp>

using graph_t = boost::adjacency_list<boost::vecS, boost::vecS, boost::directedS,
      boost::no_property,
      boost::property<boost::edge_weight_t, double> >;

int main() {
    graph_t g(5);
    boost::property_map<graph_t, boost::edge_weight_t>::type weightmap = boost::get(boost::edge_weight, g);
}

你可以稍微简化一下:

auto weightmap = get(boost::edge_weight, g);

或者考虑使用捆绑属性:

Live On Coliru

#include <boost/graph/adjacency_list.hpp>

struct EdgeProps {
    double weight = 0.0;
};

using graph_t = boost::adjacency_list<boost::vecS, boost::vecS, boost::directedS, boost::no_property, EdgeProps>;

int main() {
    graph_t g(5);
    auto weightmap = get(&EdgeProps::weight, g);
}

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2019-02-19
    • 2014-07-01
    • 2016-07-16
    相关资源
    最近更新 更多