Question

I am trying to set up a Boost Graph of vertices with bundled properties. Unfortunately, the "add_edge" step doesn't seem to work resulting in the following error. Any insights would be helpful!

error: constructor for 'boost::detail::adj_list_gen<boost::adjacency_list<boost::vecS, boost::vecS, boost::undirectedS, Node,boost::no_property, boost::no_property, boost::listS>, boost::vecS, boost::vecS, boost::undirectedS, Node, boost::no_property, boost::no_property, boost::listS>::config::rand_stored_vertex' must explicitly initialize the member 'm_property' which does not have a default constructor rand_stored_vertex() { }

and a series of other such statements culminating in the following:

in instantiation of function template specialization 'boost::add_edge<boost::adjacency_list<boost::vecS, boost::vecS, boost::undirectedS, Node, boost::no_property,boost::no_property, boost::listS>, boost::detail::adj_list_gen<boost::adjacency_list<boost::vecS, boost::vecS, boost::undirectedS, Node, boost::no_property, boost::no_property,boost::listS>, boost::vecS, boost::vecS, boost::undirectedS, Node, boost::no_property, boost::no_property, boost::listS>::config,boost::undirected_graph_helper<boost::detail::adj_list_gen<boost::adjacency_list<boost::vecS, boost::vecS, boost::undirectedS, Node, boost::no_property, boost::no_property, boost::listS>,boost::vecS, boost::vecS, boost::undirectedS, Node, boost::no_property, boost::no_property, boost::listS>::config> >' requested here boost::add_edge( svlist[0], svlist[1], SG );

class Node {
public: 
  std::string id;
  Node(int a) {
    id = std::to_string(a);
  } 
};

std::vector<Node> nodelist;
Node n1(1);
Node n2(2);
Node n3(3);
Node n4(4);
nodelist.push_back(n1);
nodelist.push_back(n2);
nodelist.push_back(n2);
nodelist.push_back(n4);

typedef boost::adjacency_list< boost::vecS, boost::vecS, 
                                      boost::undirectedS, Node> ScafGraph;
typedef boost::graph_traits<ScafGraph>::vertex_descriptor SV;
typedef boost::graph_traits<ScafGraph>::edge_descriptor SE;

ScafGraph SG;

std::vector<SV> svlist;
for(int i=0; i<4; i++) {
    SV v = boost::add_vertex( nodelist[i], SG );
    svlist.push_back(v);
}

SE e; bool ok;
boost::tie(e, ok) = boost::edge( svlist[0], svlist[1], SG );
if(!ok) {
    boost::add_edge( svlist[0], svlist[1], SG );
}
Was it helpful?

Solution

You have some errors in the Node class. Tested in GCC 4.9.0 with C++11 in Windows.

class Node {
public:
    std::string id;
    Node(int a) { id = std::to_string(a); }
    Node() = default;
};
  • The parenthesis after Node are wrong.
  • The constructor that receive an int and is converted to string, in the conversion the parameter was not used.
  • Adding a vertex or a edge to a graph when you are using vecS require that the Node used has default constructor, because the vector could need to grow and reallocate the elements, when it grow the elements are default constructed. In the case of your custom Node, don't have default constructor.
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top