无法将Boost :: Python的__str__特殊方法接口[重复]

问题描述 投票:0回答:1

此问题已经在这里有了答案:

[我正在尝试使用Boost :: Python将某些C ++代码链接到Python对象的__str__特殊方法。

#include <boost/python.hpp>
#include <iostream>

using namespace std;

struct POINT
{
    int x,y;
    POINT(int x, int y) : x(x), y(y) {}          
};

ostream & operator<<(ostream & os, POINT p)
{
    os << "(x:" << p.x << ", y:" << p.y << ")";
    return os;
}

using namespace boost::python;

BOOST_PYTHON_MODULE(wrapper)
{
    class_<POINT>("point", init<int,int>())
        .def(str(self));
}

official doc启发。这会产生一个神秘的编译错误:

    In file included from /usr/include/boost/python/object_core.hpp:20,
                 from /usr/include/boost/python/args.hpp:22,
                 from /usr/include/boost/python.hpp:11,
                 from wrapper.cpp:1:
/usr/include/boost/python/def_visitor.hpp: In instantiation of ‘static void boost::python::def_visitor_access::visit(const V&, classT&) [with V = boost::python::def_visitor<boost::python::api::object>; classT = boost::python::class_<POINT>]’:
/usr/include/boost/python/def_visitor.hpp:67:34:   required from ‘void boost::python::def_visitor<DerivedVisitor>::visit(classT&) const [with classT = boost::python::class_<POINT>; DerivedVisitor = boost::python::api::object]’
/usr/include/boost/python/class.hpp:221:9:   required from ‘boost::python::class_<T, X1, X2, X3>::self& boost::python::class_<T, X1, X2, X3>::def(const boost::python::def_visitor<Derived>&) [with Derived = boost::python::api::object; W = POINT; X1 = boost::python::detail::not_specified; X2 = boost::python::detail::not_specified; X3 = boost::python::detail::not_specified; boost::python::class_<T, X1, X2, X3>::self = boost::python::class_<POINT>]’
wrapper.cpp:49:20:   required from here
/usr/include/boost/python/def_visitor.hpp:31:9: error: no matching function for call to ‘boost::python::api::object::visit(boost::python::class_<POINT>&) const’
         v.derived_visitor().visit(c);
         ^
In file included from /usr/include/boost/python/args.hpp:22,
                 from /usr/include/boost/python.hpp:11,
                 from wrapper.cpp:1:
/usr/include/boost/python/object_core.hpp:160:12: note: candidate: ‘template<class ClassT, class DocStringT> void boost::python::api::object_operators<U>::visit(ClassT&, const char*, const boost::python::detail::def_helper<DocStringT>&) const [with ClassT = ClassT; DocStringT = DocStringT; U = boost::python::api::object]’
       void visit(ClassT& cl, char const* name, python::detail::def_helper<DocStringT> const& helper) const
            ^~~~~
/usr/include/boost/python/object_core.hpp:160:12: note:   template argument deduction/substitution failed:
In file included from /usr/include/boost/python/object_core.hpp:20,
                 from /usr/include/boost/python/args.hpp:22,
                 from /usr/include/boost/python.hpp:11,
                 from wrapper.cpp:1:
/usr/include/boost/python/def_visitor.hpp:31:9: note:   candidate expects 3 arguments, 1 provided
         v.derived_visitor().visit(c);

一个比我更开明的灵魂能指出我所缺少的吗?

python c++ boost-python
1个回答
0
投票

[self需要定义为self_ns命名空间

BOOST_PYTHON_MODULE(wrapper)
{
    class_<POINT>("point", init<int, int>())
        .def(self_ns::str(self_ns::self));
}

无关:请毫不犹豫地将输出流定义为const reference

ostream& operator<<(ostream& os, const POINT & p)
{
    os << "(x:" << p.x << ", y:" << p.y << ")";
    return os;
}
© www.soinside.com 2019 - 2024. All rights reserved.