Reputation: 113
I'm creating a simple graph using boost library, and trying to iterate over the vertices and edges. But I'm receiving following error, where the code can iterate over all vertices but there is a compilation problem when I added the last segment of code which responsible for iterating over edges:
error: conversion from ‘std::pair, boost::detail::out_edge_iter >, long unsigned int, boost::detail::edge_desc_impl, long int>, boost::adjacency_list >, boost::detail::adj_list_edge_iterator, boost::detail::out_edge_iter >, long unsigned int, boost::detail::edge_desc_impl, long int>, boost::adjacency_list > >’ to non-scalar type ‘std::pair, boost::detail::out_edge_iter<__gnu_cxx::__normal_iterator*, std::vector, std::allocator > > >, long unsigned int, boost::detail::edge_desc_impl, long int>, boost::adjacency_list<> >, boost::detail::adj_list_edge_iterator, boost::detail::out_edge_iter<__gnu_cxx::__normal_iterator*, std::vector, std::allocator > > >, long unsigned int, boost::detail::edge_desc_impl, long int>, boost::adjacency_list<> > >’ requested boost::adjacency_list<>::edge_iterator> es = boost::edges(g);
Here is my code
#include <iostream>
#include <boost/graph/adjacency_list.hpp>
#include <boost/graph/graphviz.hpp>
int main()
{
using namespace std;
using namespace boost;
typedef adjacency_list< listS, vecS, directedS > digraph;
// instantiate a digraph object with 8 vertices
digraph g(8);
// add some edges
add_edge(0, 1, g);
add_edge(1, 5, g);
add_edge(5, 6, g);
add_edge(2, 3, g);
add_edge(2, 4, g);
add_edge(3, 5, g);
add_edge(4, 5, g);
add_edge(5, 7, g);
// represent graph in DOT format and send to cout
write_graphviz(cout, g);
std::cout<< "+++++++++++++++++++++++++++++++++++++\n";
std::cout<<"Print Vertices\n";
std::pair<boost::adjacency_list<>::vertex_iterator,
boost::adjacency_list<>::vertex_iterator> vs = boost::vertices(g);
std::copy(vs.first, vs.second,std::ostream_iterator<boost::adjacency_list<>::vertex_descriptor>{
std::cout, "\n"});
std::cout<< "+++++++++++++++++++++++++++++++++++++\n";
std::cout<<"Print Edges\n";
std::pair<boost::adjacency_list<>::edge_iterator,
boost::adjacency_list<>::edge_iterator> es = boost::edges(g);
std::copy(es.first, es.second,std::ostream_iterator<boost::adjacency_list<>::edge_descriptor>{
std::cout, "\n"});
return 0;
}
Upvotes: 2
Views: 379
Reputation: 790
You're assigning the result of edges
to a variable of incompatible type. Instead of
std::pair<boost::adjacency_list<>::edge_iterator,
boost::adjacency_list<>::edge_iterator> es = boost::edges(g);
use
auto es = boost::edges(g);
or
std::pair<digraph::edge_iterator, digraph::edge_iterator> es = boost::edges(g);
More details: your type digraph
specifies three template arguments for adjacency_list
:
typedef adjacency_list< listS, vecS, directedS > digraph;
You call edges
with an argument of this type, but you specify that the function's EdgeIterator
template parameter must be boost::adjacency_list<>::edge_iterator
, as if g
were of type adjacency_list<>
instead of adjacency_list<listS, vecS, directedS>
. Apparently the conversion is valid for adjacency_list<>::vertex_iterator
but not adjacency_list<>::edge_iterator
.
Upvotes: 4