Boost.Python-包装C ++重载运算符的问题

时间:2019-04-03 15:38:39

标签: python c++ python-3.x

我使用Boost.Python,试图包装一个运算符重载方法“ +”。

我从https://www.tutorialspoint.com/cplusplus/cpp_overloading.htm获得了以下课程:

 class Box {
   public:
      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;
      }

      Box operator+(const Box& b) {
         Box box;
         box.length = this->length + b.length;
         box.breadth = this->breadth + b.breadth;
         box.height = this->height + b.height;
         return box;
      }


   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 )
    .def(self + Box()); 

}

我收到此错误:

test.cpp:47:10:错误:在此范围内未声明“ self”      .def(self + Box());

根据https://www.boost.org/doc/libs/1_40_0/libs/python/doc/tutorial/doc/html/python/exposing.html#python.class_operators_special_functions,我找不到我做错的事情。

有什么建议吗?

谢谢!

操作系统:Fedora 29-64位。

0 个答案:

没有答案