简体   繁体   English

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

[英]Boost.Python - error with parametrized constructor

I have got a toy class from tutorialspoint in a file test.h: 我在文件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
};

In the another file I have: 在另一个文件中,我有:

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

When I compile this code I get the error message about the Box class constructor: 当我编译此代码时,我得到有关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)
             ^

What am I missing? 我想念什么?

Do I need to write the constructor paramaters in BOOST_PYTHON_MODULE()? 我是否需要在BOOST_PYTHON_MODULE()中编写构造函数参数? If so, how to do it? 如果是这样,该怎么办?

You dont have a default constructor and you are missing the one you declared: 您没有默认的构造函数,并且缺少声明的构造函数:

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

The compiler is complaining that Box doesn't provide a default constructor BOOST_PYTHON_MODULE needs: 编译器抱怨Box没有提供BOOST_PYTHON_MODULE需要的默认构造BOOST_PYTHON_MODULE

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

Simply define one: 只需定义一个:

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

Additionnaly, you can checkout mohabouje's answer. 此外,您可以查看mohabouje的答案。

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

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