Pothon问题包装C++超载运算

2024-10-01 02:40:34 发布

您现在位置:Python中文网/ 问答频道 /正文

使用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的说法,我找不到我做错了什么

有什么建议吗

谢谢

OS:Fedora29-64位


Tags: ofboxdefthislengthclassdoubleheight