我可以用我自己的类包装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 作为我班级的成员?
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?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
如果您没有指定任何其他内容作为初始值设定项,则类的对象成员将使用默认构造函数进行初始化。 adjacency_matrix 似乎没有默认构造函数。
你应该这样做:
typedef boost::adjacency_matrix Graph;
当然,在实践中,您还可以使用其他构造函数参数,使用您赢得的默认值等。这应该只是一个非常简单的示例。我从未使用过 boost::adjecency_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.
You should do something like this instead:
typedef boost::adjacency_matrix Graph;
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.
编译器告诉您 boost::adjacency_matrix 没有不带参数的构造函数,因此它无法将其构造为您的类的成员。类的对象成员必须在构造包含类的同时构造,除非您另外指定,否则编译器假定存在一个可以调用的不带参数的构造函数。如果不存在,您会收到这样的错误。
解决方案是在构造函数的初始值设定项列表中为 g 的构造函数提供参数:
其中 ... 是适当的构造函数参数,无论 adjacency_matrix 的构造函数之一为您提供所需的对象。这将覆盖编译器尝试调用 0 参数构造函数的默认本能,并使其调用与您提供的参数匹配的任何一个构造函数。
如果您不想在类的构造时执行此操作,则必须存储指向它的指针并在适当的时间使用 new 构造它。或者更好的是,向其中存储一个shared_ptr,这样您就不必担心在您的类被销毁时删除它。
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. 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:
Where the ... is the appropriate constructor parameters for whatever one of adjacency_matrix's constructors gets you the object you want. 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.
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. Or better, store a shared_ptr to it so you don't have to worry about deleting it when your class is destroyed.
据我了解,Graph 没有默认的构造函数。
您应该提供一个参数,因此只需参考文档。
As far as I understand there is no default constructor for Graph.
You should provide a parameters, so just refer to documentation.