我在文件test.h中有一个来自tutorialspoint的玩具课:
class Box {
public:
Box(int l, int b, int h)
{
length = l;
breadth = b;
height = h;
}
double getVolume(void) {
return length * breadth * height;
}
void setLength( double len ) {
length = len;
}
void setBreadth( double bre ) {
breadth = bre;
}
void setHeight( double hei ) {
height = hei;
}
private:
double length; // Length of a box
double breadth; // Breadth of a box
double height; // Height of a box
};
在另一个文件中,我有:
BOOST_PYTHON_MODULE(test)
{
namespace python = boost::python;
python::class_<Box>("Box")
.def("setLength", &Box::setLength )
.def("setBreadth", &Box::setBreadth)
.def("setHeight", &Box::setHeight )
.def("getVolume", &Box::getVolume );
}
编译此代码时,我收到有关Box类构造函数的错误消息:
/usr/include/boost/python/object/value_holder.hpp:133:13: error: no matching function for call to ‘Box::Box()’
BOOST_PP_REPEAT_1ST(N, BOOST_PYTHON_UNFORWARD_LOCAL, nil)
^
我想念什么?
是否需要在BOOST_PYTHON_MODULE()中编写构造函数参数?如果是这样,该怎么做?
答案 0 :(得分:2)
您没有默认的构造函数,而缺少声明的构造函数:
BOOST_PYTHON_MODULE(test) {
namespace python = boost::python;
python::class_<Box>("Box", boost::python::init<int, int, int>())
.def("setLength", &Box::setLength )
.def("setBreadth", &Box::setBreadth)
.def("setHeight", &Box::setHeight )
.def("getVolume", &Box::getVolume );
}
答案 1 :(得分:1)
编译器抱怨Box
没有提供默认的构造函数BOOST_PYTHON_MODULE
需要:
no matching function for call to ‘Box::Box()
只需定义一个:
class Box {
public:
Box() = default;
// [...]
};
此外,您可以查看mohabouje的答案。