Boost Djikstra 类 "boost::p roperty_map> 没有成员 "type"

Boost Djikstra class "boost::property_map> has no member "type"

本文关键字:gt type 成员 Djikstra boost Boost roperty map      更新时间:2023-10-16

一段时间以来,我一直在努力使用 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"

我找不到其他人有同样的问题,而且我似乎无法弄清楚问题是什么。关于这个问题的任何帮助将不胜感激。

提前致谢

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

住在科里鲁

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);

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

住在科里鲁

#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);

或者考虑使用捆绑属性:

住在科里鲁

#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);
}