我使用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());
有什么建议吗?
谢谢!
操作系统:Fedora 29-64位。