简体   繁体   中英

How do you access edge properties in the boost graph library

I am trying to write a program that wraps the boost graph library so as to provide a convenient user interface. I am very new to boost (and stack overflow) but have spent quite a bit of time reading the boost documentation.

My problem is that whenever I go to get a property map for my edge properties, whether I am using bundled properties or internal properties, I get an error when ever call the get() function for an edge property saying that there is no function prototype that matches the given arguments. I have no difficulty when doing the same thing for my vertex properties. Moreover, the boost docs seem to also indicate that there is no get() function for edge properties. I think the problem may be in my constructor, which is designed to read graph information from a text file:

BGraph::BGraph()                                                           // constructor
{ 
    graph;          // is this how you would initialize the graph?
    //...input and initialization of vertices

    //...input of edge information

    auto e = add_edge(vertex1, vertex2, graph).first;                  // add the edge

    // this is the internal property version
    property_map<Graph, edge_weight_t>::type weightMap = get(edge_weight, graph);
                                    // ^^^^^ error here saying there is no such member
    weightMap[e] = inWeight;

    //graph[e].weight = inWeight;       this is the bundled version    // give the edge its weight

}

}

Here is my header file for the wrapper class:

#include <boost/config.hpp>
#include <boost/graph/adjacency_list.hpp>
#include <boost/graph/graph_traits.hpp>
#include <boost/graph/adjacency_iterator.hpp>
#include <boost/graph/dijkstra_shortest_paths.hpp>
#include <boost/graph/kruskal_min_spanning_tree.hpp>
#include <boost/graph/named_function_params.hpp>
#include <boost/graph/properties.hpp>
#include <boost/property_map/property_map.hpp>
#include <boost/graph/graphviz.hpp>

#include <string>
#include <vector>
#include <iostream>

using namespace std;
using namespace boost;

class BGraph
{
public:

    struct Edge_Properties                                      // property bundle for edges
    {
        string name;
        int weight = 1;
    };

    struct Vertex_Properties                                    // property bundle for vertices
    {
        string name;
        int distance;
        int pred;
    };

    // class member functions...

    typedef adjacency_list<vecS, vecS, bidirectionalS,          // graph type
        Vertex_Properties, property<edge_weight_t, int> > Graph;

 /*typedef adjacency_list<vecS, vecS, bidirectionalS,           // graph type
    Vertex_Properties, Edge_Properties> Graph;*/                // this is the bundled version

    typedef property_map<Graph, vertex_index_t>::type IdMap;
    typedef typename graph_traits<Graph>::vertex_descriptor vertex_descriptor;
private:
    Graph graph;                                                // the boost graph
}

I have similar problems in my function for dijkstra_shortest_paths :

dijkstra_shortest_paths(graph, findVertex(startVertex), 
    predecessor_map(get(&Vertex_Properties::pred, graph))
    .distance_map(get(&Vertex_Properties::distance, graph))
    .weight_map(/*get(&Edge_Properties::weight, graph)*/ get(edge_weight, graph)));

The specific error on the get function is as follows:

no instance of overloaded function "get" matches the argument list. argument types are: (boost:edge_weight_t, const BGraph::graph)

I feel like there is some overly simple solution, but I just have not been able to find it. I am using MS Visual Studio 2017 and boost version boost_1_67_0. I think the problem may have something to do with Visual Studio in particular because code almost identical to mine seems to work for other people. Thanks for your help.

It seems that your code should work for internal properties.

When you use bundled properties you need to specify PropertyTag for property_map as pointer to data member, in your case pointer to weight member of Edge_properties .

    property_map<Graph,int Edge_Properties::*>::type weightMap = 
                   get(&Edge_Properties::weight, graph);
                     //^^^^^
    auto e = add_edge(1, 2, graph).first;
    weightMap[e] = 10;
    graph[e].weight = 20;   

Here are examples of getting property map from bundled properties. You can easily adapt them to your needs.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM