c++ - 我可以用我自己的类(class)包装升压图吗

标签 c++ boost-graph

我正在尝试将 boost::adjacency_matrix 图作为成员创建我自己的类,但我遇到了编译错误。无法编译的示例类:

namespace zto {

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

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

编译错误:

./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

看起来编译器认为 Graph 是一个函数?!

谁能告诉我,如何将 boost::adjacency_matrix 声明为我类(class)的成员?

最佳答案

如果您没有指定其他任何东西作为初始化器,您的类的对象成员将使用默认构造函数进行初始化。 adjacency_matrix 似乎没有默认构造函数。

你应该做这样的事情:

typedef boost::adjacency_matrix 图;

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.
    }

};

当然,在实践中您还可以使用其他构造函数参数,使用您赢得的默认值等。这应该只是一个非常简单的示例。我从来没有使用过 boost::adjecency_matrix 所以我不知道好的做法和真正有意义的东西。我只是从错误消息中提取的。

关于c++ - 我可以用我自己的类(class)包装升压图吗,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/7953237/

相关文章:

c++ - copy_graph - 具有捆绑属性的 adjacency_list

c++ - 为什么默认构造函数在某些情况下默认不可用

c++ - OpenCV calibrateCamera 断言失败(ni == ni1)

c++ - Aruco 函数 estimatePoseSingleMarkers() 错误

c++ - 为什么 BGL A* 需要隐式图来建模 VertexListGraph?

c++ - 提升图形库 : possible to combine Bundled Properties with Interior Properties?

c++ - 提供从根到每个分支端的所有顶点路由的访问者

c++ - 如何在 C++ 中的低优先级线程中运行方法?

c++ - 如何在 C++ 中实现信号和事件?

c++ - boost::get with boost::filtered_graph on adjacency_list with netsed 属性