boostpython的通用异常转换 当前升压::将特定C++异常翻译成Python的Python示例如下: void translate (const MyException& e) { PyErr_SetString(PyExc_RuntimeError, e.what()); } boost::python::register_exception_translator<MyException>(translate); BOOST_PYTHON_MODULE(libtest) { translate<std::out_of_range>(PyExc_RuntimeError); }

boostpython的通用异常转换 当前升压::将特定C++异常翻译成Python的Python示例如下: void translate (const MyException& e) { PyErr_SetString(PyExc_RuntimeError, e.what()); } boost::python::register_exception_translator<MyException>(translate); BOOST_PYTHON_MODULE(libtest) { translate<std::out_of_range>(PyExc_RuntimeError); },c++,exception-handling,boost-python,C++,Exception Handling,Boost Python,在我们的通用异常处理程序中,这将是对GeneratedTranslator::operator()的调用,对于g++不起作用,给出: error: ‘translate’ cannot be used as a function 是否有正确的编写方法?您正在将this指针作为translate函数传递,但由于指向对象的指针不能作为函数调用,因此此操作失败。如果您传递*此,则应该可以工作(请注意,这将复制并构造GeneratedTranslator对象)。或者您可以将注册移出构造函数,并调用 r

在我们的通用异常处理程序中,这将是对GeneratedTranslator::operator()的调用,对于g++不起作用,给出:

error: ‘translate’ cannot be used as a function

是否有正确的编写方法?

您正在将
this
指针作为translate函数传递,但由于指向对象的指针不能作为函数调用,因此此操作失败。如果您传递
*此
,则应该可以工作(请注意,这将复制并构造GeneratedTranslator对象)。或者您可以将注册移出构造函数,并调用

register_exception_translator<std::out_of_range>(GeneralizedTranslator<std::out_of_range>(PyExc_RuntimeError));
register\u exception\u转换器(GeneralizedTranslator(PyExc\u RuntimeError));

是。你说对了!我已经更新了上面的代码,所以它显示了正确的解决方案。谢谢,A
error: ‘translate’ cannot be used as a function
register_exception_translator<std::out_of_range>(GeneralizedTranslator<std::out_of_range>(PyExc_RuntimeError));