【问题标题】:Boost.Graph Reference to voidBoost.Graph 对 void 的引用
【发布时间】:2016-06-02 22:25:04
【问题描述】:

我想使用来自 Boost.Graph 库的push_relabel_max_flow。 我已经生成了我的图表,这是我目前的代码:

    struct EdgeProps {
        double capacity;
        double residual_capacity;
        Traits::edge_descriptor reverse;
    };

    typedef boost::adjacency_list<boost::vecS, boost::vecS, boost::directedS, boost::no_property, EdgeProps > DirectedGraph;

    DirectedGraph g;
    std::vector<DirectedGraph::vertex_descriptor> vertices;

    /* Filling the Graph with vertices and edges and saving the vertex-descriptors in "vertices" */
    //...
    //...

double flow = boost::push_relabel_max_flow(g,vertices[0],vertices[1],
                    vertex_index_map(boost::get(boost::vertex_index, g)).
                     residual_capacity_map(boost::get(&EdgeProps::residual_capacity, g)).
                      reverse_edge_map(boost::get(&EdgeProps::reverse, g)).
                       capacity_map(boost::get(&EdgeProps::capacity, g))
                    );

我在传递参数时遇到问题。我得到“形成对 void 的引用”-错误:

    /usr/local/include/boost/graph/detail/adjacency_list.hpp:2696: error: forming reference to void
             typedef value_type& reference;
                                 ^

/usr/local/include/boost/graph/detail/adjacency_list.hpp:2697: error: forming reference to void
         typedef const value_type& const_reference;
                                   ^

/usr/local/include/boost/graph/detail/adjacency_list.hpp:2701: error: forming reference to void
             typename Graph::vertex_descriptor,Property,Tag> type;
                                                             ^

这些不是全部,只是其中三个。如果您需要查看每一个,请发表评论,我会添加它们。 有谁知道我必须如何将参数传递给函数而不产生“Reference to void”-错误?

【问题讨论】:

    标签: c++ boost boost-graph


    【解决方案1】:

    boost::capacity_map 之类的函数可以利用 named parameter idiom that the Boost Graph library provides。这些函数返回一个bgl_named_params 的实例,它具有添加更多命名参数的方法。因此,不是用逗号分隔参数,而是用点分隔它们;对于第一个之后的参数,不需要boost::,因为它们是成员方法。

    您最初的问题试图这样称呼:

    double flow = boost::push_relabel_max_flow(g, vertices[0], vertices[1],
                        boost::capacity_map(boost::get(&EdgeProps::capacity, g)),
                         boost::residual_capacity_map(boost::get(&EdgeProps::residual_capacity, g)),
                          boost::reverse_edge_map(boost::get(&EdgeProps::reverse, g)),
                           boost::vertex_index_map(boost::get(boost::vertex_index, g)));
    

    不支持像这样传递bgl_named_params 的多个实例:它应该看起来像

    double flow = boost::push_relabel_max_flow(g, vertices[0], vertices[1],
                        boost::capacity_map(boost::get(&EdgeProps::capacity, g)).
                         residual_capacity_map(boost::get(&EdgeProps::residual_capacity, g)).
                          reverse_edge_map(boost::get(&EdgeProps::reverse, g)));
    

    vertex_index_map 参数被省略。 只要图中的 VertexList 是 std::vector(即 adjacency_list 的第二个模板参数是 boost::vecS),vertex_index 属性就会自动存在,并且默认参数应该可以工作。

    但是,最大流量算法的命名参数在当前版本的 Boost (1.61) 中不起作用。 这是Boost Issue 12038,5 月 2 日是 fixed in the development branch。我可以确认上面的示例适用于当前的开发分支。

    使用该函数的 7 参数版本并传递所有必需的映射确实有效,但此版本不支持默认参数,因此您必须指定 vertex_index 映射。例如:

    auto capacity = boost::get(&EdgeProps::capacity, g);
    auto reverse  = boost::get(&EdgeProps::reverse, g);
    auto residcap = boost::get(&EdgeProps::residual_capacity, g);
    auto indexmap = boost::get(boost::vertex_index, g);
    
    double flow = boost::push_relabel_max_flow(g, vertices[0], vertices[1],
                        capacity, residcap, reverse, indexmap);
    

    【讨论】:

    • 谢谢你,它现在正在工作。我应该在下次打开问题之前搜索任何错误报告。
    【解决方案2】:

    据我所知,命名参数的解包代码中肯定有一个错误,因为以下编译干净:

    Live On Coliru

    #include <boost/graph/adjacency_list.hpp>
    #include <boost/graph/push_relabel_max_flow.hpp>
    
    typedef boost::graph_traits<boost::adjacency_list<boost::vecS, boost::vecS, boost::directedS> > Traits;
    
    struct EdgeProps {
        double capacity;
        double residual_capacity;
        Traits::edge_descriptor reverse;
    };
    
    typedef boost::adjacency_list<boost::vecS, boost::vecS, boost::directedS, boost::no_property, EdgeProps> DirectedGraph;
    
    int main() {
        DirectedGraph g;
        std::vector<DirectedGraph::vertex_descriptor> vertices;
    
        /* Filling the Graph with vertices and edges and saving the vertex-descriptors in "vertices" */
        //...
        //...
    
        double flow = boost::push_relabel_max_flow(g, vertices[0], vertices[1],
                boost::get(&EdgeProps::capacity, g),
                boost::get(&EdgeProps::residual_capacity, g),
                boost::get(&EdgeProps::reverse, g),
                boost::get(boost::vertex_index, g)
            );
    }
    

    您可能想将此情况报告给库开发人员。

    【讨论】:

    猜你喜欢
    • 2020-03-21
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2017-09-03
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2022-06-15
    相关资源
    最近更新 更多