为Boost Python C++类添加`__str__`方法时构建问题
我最近开始玩弄一下boost python,但遇到了一些问题。我试着把一个C++类暴露给Python,这个过程没有什么问题。但是,我在实现这个类的__str__
功能时,总是出现一些我不明白的构建错误。
我使用的是boost 1_42,由boostpro预编译的版本。我用cmake和vs2010编译这个库。
我的设置非常简单。头文件(tutorial.h)看起来是这样的:
#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文件看起来是:
#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() << "\n";
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文件看起来是这样的:
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}
我遇到的构建错误如下:
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中使用。我会非常感激任何帮助。
谢谢,
Rickard
编辑:忘记加一个const了
2 个回答
11
我遇到过同样的问题。加上这一行(不需要在前面加上str和self)也能解决:
using self_ns::str;
34
我最近遇到了一个问题;
解决这个问题的方法是明确地在这一行中处理 str
和 self
:
.def(str(self))
这样就变成了:
.def(self_ns::str(self_ns::self))
我不知道为什么这样做是必要的,(我对 Boost Python 中的重载解析有一点了解,可能是那里的问题……)但这个方法对我有效 :)