2017-08-07 60 views
1

我正在使用boost庫創建一個簡單的圖形,並試圖遍歷頂點和邊。不過,我收到以下錯誤,其中的代碼可以遍歷所有頂點,但有一個編譯問題,當我加入的代碼的最後一段,其負責遍歷邊緣:Boost圖形轉換錯誤

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);

這裏是我的代碼

#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; 
} 

回答

4

您將edges的結果分配給不兼容類型的變量。取而代之的

std::pair<boost::adjacency_list<>::edge_iterator, 
    boost::adjacency_list<>::edge_iterator> es = boost::edges(g); 

使用

auto es = boost::edges(g); 

std::pair<digraph::edge_iterator, digraph::edge_iterator> es = boost::edges(g); 

更多細節:你的類型digraph指定adjacency_list 3個模板參數:

typedef adjacency_list< listS, vecS, directedS > digraph; 

你叫edges這種類型的參數,但是您可以指定函數的EdgeIterator模板參數必須是boost::adjacency_list<>::edge_iterator,彷彿gadjacency_list<>類型,而不是adjacency_list<listS, vecS, directedS>的。顯然該轉換適用於adjacency_list<>::vertex_iterator,但不是adjacency_list<>::edge_iterator

+0

謝謝,通過使用std :: pair es = boost :: edges(g); – Mohannad