Build problems when adding `__str__` method to Boost Python C++ class
我已经开始尝试使用boost python并遇到问题。 我试图将C ++类公开给python,这没有任何问题。 但是我似乎无法在没有出现我不理解的构建错误的情况下设法为该类实现
我正在使用boostpro的boost 1_42 prebuild。 我使用cmake和vs2010编译器来构建库。
我有一个非常简单的设置。 头文件(tutorial.h)如下所示:
1 2 3 4 5 6 7 8 9 10 11 12 | #include <iostream> namespace TestBoostPython{ class TestClass { private: double m_x; public: TestClass(double x); double Get_x() const; void Set_x(double x); }; std::ostream &operator<<(std::ostream &ostr, const TestClass &ts); }; |
相应的cpp文件如下所示:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 | #include <boost/python.hpp> #include"tutorial.h" using namespace TestBoostPython; TestClass::TestClass(double x) { m_x = x; } double TestClass::Get_x() const { return m_x; } void TestClass::Set_x(double x) { m_x = x; } std::ostream &operator<<(std::ostream &ostr, const TestClass &ts) { ostr << ts.Get_x() <<"\ "; return ostr; } BOOST_PYTHON_MODULE(testme) { using namespace boost::python; class_<TestClass>("TestClass", init<double>()) .add_property("x", &TestClass::Get_x, &TestClass::Set_x) .def(str(self)) ; } |
CMakeLists.txt如下所示:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 | CMAKE_MINIMUM_REQUIRED(VERSION 2.8) project (testme) FIND_PACKAGE( Boost REQUIRED ) FIND_PACKAGE( Boost COMPONENTS python REQUIRED ) FIND_PACKAGE( PythonLibs REQUIRED ) set(Boost_USE_STATIC_LIBS OFF) set(Boost_USE_MULTITHREAD ON) INCLUDE_DIRECTORIES(${Boost_INCLUDE_DIRS}) INCLUDE_DIRECTORIES ( ${PYTHON_INCLUDE_PATH} ) add_library(testme SHARED tutorial.cpp) target_link_libraries(testme ${Boost_PYTHON_LIBRARY}) target_link_libraries(testme ${PYTHON_LIBRARY} |
我得到的生成错误如下:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 | Compiling... tutorial.cpp C:\\Program Files (x86)\\boost\\boost_1_42\\boost/python/def_visitor.hpp(31) : error C2780: 'void boost::python::api::object_operators::visit(ClassT &,const char *,const boost::python::detail::def_helper &) const' : expects 3 arguments - 1 provided with [ U=boost::python::api::object ] C:\\Program Files (x86)\\boost\\boost_1_42\\boost/python/object_core.hpp(203) : see declaration of 'boost::python::api::object_operators::visit' with [ U=boost::python::api::object ] C:\\Program Files (x86)\\boost\\boost_1_42\\boost/python/def_visitor.hpp(67) : see reference to function template instantiation 'void boost::python::def_visitor_access::visit,classT>(const V &,classT &)' being compiled with [ DerivedVisitor=boost::python::api::object, classT=boost::python::class_, V=boost::python::def_visitor ] C:\\Program Files (x86)\\boost\\boost_1_42\\boost/python/class.hpp(225) : see reference to function template instantiation 'void boost::python::def_visitor::visit>(classT &) const' being compiled with [ DerivedVisitor=boost::python::api::object, W=TestBoostPython::TestClass, classT=boost::python::class_ ] .\\tutorial.cpp(29) : see reference to function template instantiation 'boost::python::class_ &boost::python::class_::def(const boost::python::def_visitor &)' being compiled with [ W=TestBoostPython::TestClass, U=boost::python::api::object, DerivedVisitor=boost::python::api::object ] |
有谁知道发生了什么问题吗? 如果我从包装器代码中删除.def(str(self))部分,则一切都可以正常编译,并且该类可从python使用。 我将非常感谢您的协助。
谢谢,
里卡德
编辑:忘记一个常量
我最近遇到了这个问题;
有效的解决方案是在此行上显式解析
1 | .def(str(self)) |
这样就变成:
1 | .def(self_ns::str(self_ns::self)) |
我不知道为什么这是必要的,(知道在boost python中发生的过载解析复杂性,可能在那儿...),但是它对我有用:)
我遇到了同样的事情。 添加此行(而不是限定str和self)也可以:
1 | using self_ns::str; |