I am trying to make a class called GraphService with the Boost Graph Library. It should compute various stuff on graphs and return properties like the node degree distribution to other classes. This is a part of my headerfile for the graph, which is called Graph.h:
typedef boost::adjacency_list<boost::vecS, boost::vecS, boost::undirectedS,
vertex_info, edge_info, graph_info, boost::listS> Graph;
typedef boost::graph_traits<Graph>::vertex_descriptor VertexDescriptor;
typedef boost::graph_traits<Graph>::edge_descriptor EdgeDescriptor;
typedef boost::graph_traits<Graph>::vertex_iterator VertexIterator;
typedef boost::graph_traits<Graph>::edge_iterator EdgeIterator;
typedef boost::graph_traits<Graph>::adjacency_iterator AdjacencyIterator;
And this is a part of my Headerfile for GraphService:
#include "Graph.h"
class GraphService {
private:
Graph g;
public:
std::vector<int> get_adjacent(int i);
}
The part that is troubling me, is this:
#include "Graph.h"
GraphService::GraphService(Graph graph) {
g = graph
}
std::vector<int> GraphService::get_adjacent(int i) {
AdjacencyIterator first, last;
std::vector<int> vertex_vector;
vertex_vector.push_back(i);
for (tie(first,last) = adjacent_vertices(i,g); first != last; ++first) {
vertex_vector.push_back(g[*first].id);
}
return vertex_vector;
}
I tested this function with UnitTests and the tests work correctly, but in my compiler output, I see the following error-message;
/bin/sh: line 7: 20148 Segmentation fault build/Debug/GNU-Linux-x86/tests/TestFiles/f2
This seems to refer to fault with the adjacency_iterator. The Debugger tells me, that the error occurs, because of the iterator last.
I have tried to run the same code in a script where the creation of the graph and the iteration over adjacent_vertices are in the same function. Everything works correctly there.
Can anyone help?
Thanks in advance!
I agree, especially in relation to your previous question, you probably have errors (Undefined Behaviour?) outside the code shown.
The code shown could just work, see this SSCCE:
Live On Coliru
Prints, e.g.