2

I am trying to store vectors in the vertices of the boost graph. I am running into problems reading from file using the boost::read_graphviz function. Compiling breaks due to the line that relates the vertex content to the dynamic properties.

Here is a simplified version (let's call it test.cpp):

#include <iostream>
#include <vector>
#include <boost/graph/adjacency_list.hpp>
#include <boost/graph/graphviz.hpp>
#include <fstream>

struct Vertex
{
  std::vector<int> p;
};

struct Edge
{
  double w;
};


// this should be necessary, but is not 'seen' by compiler
std::ostream &
operator << ( std::ostream &OUT, const std::vector<int> &P)
  {
    OUT << P.size() << " ";
    for( int i = 0; i < P.size(); ++i)
      OUT << P[i]  << " ";
    return OUT;
  }

// also doesn't help (and also should not be necessary as ostringstream is derived from ostream (or?) 
 std::ostringstream &
 operator << ( std::ostringstream &OUT, const std::vector<int> &P)
   {
     OUT << P.size() << " ";
     for( int i = 0; i < P.size(); ++i)
       OUT << P[i] << " ";
     return OUT;
   }



int main()
{
  typedef  boost::adjacency_list<boost::setS, boost::setS, boost::bidirectionalS, Vertex, Edge>
    Graph;

  Graph
    graph;

  std::ifstream
    in;

  boost::dynamic_properties
    dp;

  dp.property("vector",  get(  &Vertex::p,  graph));  // the critical line
  dp.property("value",   get(  &Edge::w,    graph));

  boost::read_graphviz( in, graph, dp);
}

I compile this using

g++ test.cpp -lboost_graph

And get the following error message (first block only):

In file included from /usr/include/boost/graph/graphviz.hpp:25:0,
                 from boost_graph_test.cpp:4:
/usr/include/boost/property_map/dynamic_property_map.hpp: In instantiation of ‘std::string boost::detail::dynamic_property_map_adaptor<PropertyMap>::get_string(const boost
::any&) [with PropertyMap = boost::adj_list_vertex_property_map<boost::adjacency_list<boost::setS, boost::setS, boost::bidirectionalS, Vertex, Edge>, std::vector<int>, std
::vector<int>&, std::vector<int> Vertex::*>; std::string = std::basic_string<char>]’:
boost_graph_test.cpp:58:1:   required from here
/usr/include/boost/property_map/dynamic_property_map.hpp:180:9: error: no match for ‘operator<<’ (operand types are ‘std::ostringstream {aka std::basic_ostringstream<char>}’ and ‘std::vector<int>’)
     out << get_wrapper_xxx(property_map_, any_cast<typename boost::property_traits<PropertyMap>::key_type>(key));

The issue is the line marked as critical. When commented out, it compiles. What am I doing wrong?

redo
  • 25
  • 5

1 Answers1

1

This is a FAQ.

You need to declare the overload in an ADL-associated namespace. Because int has no associated namespace, you must define it in namespace std.

Make it file-static so you avoid ODR conflicts.

namespace std {

    template <T>
    static inline std::ostream& operator<<(std::ostream& os, std::vector<T> const& v) {

          // ...

etc.


Another slightly cleaner approach is to define a UDT (user-defined type) that allows you to properly hook up streaming overloads in your own namespace.

Here's a sample that shows the write_graphviz side: Using two objects as hash key for an unordered_map or alternatives

Community
  • 1
  • 1
sehe
  • 374,641
  • 47
  • 450
  • 633
  • Great, thanks. Expected a boost related issue... It works now, providing both operator << and operator >> – redo Nov 25 '16 at 13:49
  • @redo Would mind sharing the code for operator << and operator >>? – nevrome Oct 22 '17 at 16:14
  • 1
    @nevrome how about http://coliru.stacked-crooked.com/a/bb5fb13f9c2e852d – sehe Oct 22 '17 at 20:04
  • Thanks - that's a useful example for me. – nevrome Oct 22 '17 at 20:27
  • 1
    Cheers @nevrome. Don't be shy to post your own question next time. The `skipws` pitfall got me there when I made the example. That could be useful to anyone in the future – sehe Oct 22 '17 at 20:29