简体   繁体   English

我可以在自己的课上包装升压图吗

[英]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. 我正在尝试使用boost :: adjacency_matrix图作为成员来确定我自己的类,但是在编译错误时我被卡住了。 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?! 好像编译器认为Graph是一个函数?!

Can anybody tell me, how to declare boost::adjacency_matrix as a member of my class? 谁能告诉我,如何将boost :: adjacency_matrix声明为班级成员?

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. 似乎adjacency_matrix没有默认的构造函数。

You should do something like this instead: 您应该改为执行以下操作:

typedef boost::adjacency_matrix Graph; 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.
    }

};

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. 我从来没有使用过boost :: adjecency_matrix,所以我不知道好的做法和实际意义。 I just took this from the error message. 我只是从错误消息中获取了此信息。

The compiler is telling you that boost::adjacency_matrix doesn't have a constructor taking no arguments, so it's unable to construct one as a member of your class. 编译器告诉您boost :: adjacency_matrix没有构造函数不带任何参数,因此它无法构造一个作为类成员的成员。 Object members of classes have to be constructed at the same time that the containing class is constructed, and unless you specify otherwise the compiler assumes there's a constructor with no parameters that it can call. 必须在构造包含类的同时构造类的对象成员,除非您另外指定,否则编译器会假定存在没有可调用参数的构造函数。 When there isn't, you get errors like this. 如果没有,则会出现类似的错误。

The solution is to provide parameters for g's constructor in your constructor's initializer list: 解决方案是在构造函数的初始值设定项列表中为g的构造函数提供参数:

SampleClass() : g( ... )
{
}

Where the ... is the appropriate constructor parameters for whatever one of adjacency_matrix's constructors gets you the object you want. 对于任何adjacency_matrix的构造函数,...是合适的构造函数参数,您可以使用该对象来获取所需的对象。 This will override the compiler's default instinct to try to call the 0-parameter constructor, and make it call whichever one matches the parameters you supply instead. 这将覆盖编译器的默认本能,以尝试调用0参数构造函数,并使其调用与您提供的参数匹配的任何一个。

If you don't want to do it at your class's construction time, you'll have to store a pointer to it and construct it using new at the appropriate time. 如果您不想在类的构造时执行此操作,则必须存储指向它的指针,并在适当的时间使用new构造它。 Or better, store a shared_ptr to it so you don't have to worry about deleting it when your class is destroyed. 或者更好的是,将一个shared_ptr存储到它,这样您就不必担心在类被销毁时将其删除。

As far as I understand there is no default constructor for Graph. 据我了解,没有Graph的默认构造函数。

You should provide a parameters, so just refer to documentation . 您应该提供一个参数,因此请参考文档

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM