Boost.Python - 参数化构造函数错误

Boost.Python - error with parametrized constructor

我从文件中的 tutorialspoint 得到了一个玩具 class test.h:

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 class 构造函数的错误消息:

/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() 中编写构造函数参数吗?如果可以,怎么做?

编译器抱怨 Box 没有提供默认构造函数 BOOST_PYTHON_MODULE 需要:

no matching function for call to ‘Box::Box()

简单定义一个:

class Box {
public:
    Box() = default;
// [...]
};

此外,您可以查看 mohabouje 的回答。

您没有默认构造函数,并且缺少您声明的构造函数:

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 );
}