我在文件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()中编写构造函数参数?如果是这样,该怎么办?

最佳答案

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

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

关于c++ - Boost.Python-参数化构造函数出错,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/55511617/

10-11 23:06
查看更多