Search code examples
c++boost-graph

Can I wrap boost graph with my own class


I am trying to cerate my own class with boost::adjacency_matrix graph as a member, but I am stuck on compilation error. Sample class that won't compile:

namespace zto {

typedef boost::adjacency_matrix<boost::undirectedS> Graph;

class SampleClass
{
public:
    Graph g;
    SampleClass();
};

And the compilation error:

./src/sample.cpp: In constructor ‘zto::SampleClass::SampleClass()’:
./src/sample.cpp:27:31: error: no matching function for call to 
  ‘boost::adjacency_matrix<boost::undirectedS>::adjacency_matrix()’
./src/sample.cpp:27:31: note: candidates are:
/usr/include/boost/graph/adjacency_matrix.hpp:622:5: note: template<class EdgeIterator, 
  class EdgePropertyIterator> boost::adjacency_matrix::adjacency_matrix(EdgeIterator, 
  EdgeIterator, EdgePropertyIterator, boost::adjacency_matrix<Directed, VertexProperty, 
  EdgeProperty, GraphProperty, Allocator>::vertices_size_type, const GraphProperty&)
/usr/include/boost/graph/adjacency_matrix.hpp:604:5: note: template<class EdgeIterator> 
  boost::adjacency_matrix::adjacency_matrix(EdgeIterator, EdgeIterator, 
  boost::adjacency_matrix<Directed, VertexProperty, EdgeProperty, GraphProperty, 
  Allocator>::vertices_size_type, const GraphProperty&)
/usr/include/boost/graph/adjacency_matrix.hpp:593:5: note: 
  boost::adjacency_matrix<Directed, VertexProperty, EdgeProperty, GraphProperty, 
  Allocator>::adjacency_matrix(boost::adjacency_matrix<Directed, VertexProperty, 
  EdgeProperty, GraphProperty, Allocator>::vertices_size_type, const GraphProperty&) 
  [with Directed = boost::undirectedS, VertexProperty = boost::no_property, EdgeProperty 
  = boost::no_property, GraphProperty = boost::no_property, Allocator = 
  std::allocator<bool>, boost::adjacency_matrix<Directed, VertexProperty, EdgeProperty, 
  GraphProperty, Allocator>::vertices_size_type = unsigned int]
/usr/include/boost/graph/adjacency_matrix.hpp:593:5: note:   candidate expects 2 
  arguments, 0 provided
/usr/include/boost/graph/adjacency_matrix.hpp:474:9: note: 
  boost::adjacency_matrix<boost::undirectedS>::adjacency_matrix(const 
  boost::adjacency_matrix<boost::undirectedS>&)
/usr/include/boost/graph/adjacency_matrix.hpp:474:9: note:   candidate expects 1 
  argument, 0 provided

It looks like that compilator thinks Graph is a function?!

Can anybody tell me, how to declare boost::adjacency_matrix as a member of my class?


Solution

  • The object member of your class is intitialized using a default constructor if you don't specify anything else as initializer. It seems that adjacency_matrix does not have a default constructor.

    You should do something like this instead:

    typedef boost::adjacency_matrix Graph;

    class SampleClass
    {
    public:
        Graph g;
        SampleClass(size_t arg)
        : g(arg) // this is important
        {
          // As before, can also be in a cpp. 
          // It is just important that the initializer is used.
        }
    
    };
    

    Of course, in practice you could also make use of the other constructor arguments, use you won defaults, etc. This should just be a very simple example. I have never used boost::adjecency_matrix so I don't know about good practice and what actually makes sense. I just took this from the error message.