tja*_*anu 5 c++ boost boost-python
我得到一个错误,我无法通过Boost.Python包装一个相当简单的c ++类.一,班级:
#include <boost/python.hpp>
#include <boost/shared_ptr.hpp>
#include <vector>
#include <string>
class token {
public:
typedef boost::shared_ptr<token> ptr_type;
static std::vector<std::string> empty_context;
static std::string empty_center;
token(std::string& t = empty_center,
std::vector<std::string>& left = empty_context,
std::vector<std::string>& right = empty_context)
: center(t), left_context(left), right_context(right) {}
virtual ~token(void) {}
std::vector<std::string> left_context;
std::vector<std::string> right_context;
std::string center;
};
std::string token::empty_center;
std::vector<std::string> token::empty_context;
Run Code Online (Sandbox Code Playgroud)
暴露于python via
BOOST_PYTHON_MODULE(test) {
namespace bp = boost::python;
bp::class_<token, token::ptr_type>("token")
.def(bp::init<std::string&, bp::optional<std::vector<std::string>&,
std::vector<std::string>&> >())
;
}
Run Code Online (Sandbox Code Playgroud)
然后,当试图在python中使用它时:
Python 2.7.2 (default, Aug 19 2011, 20:41:43) [GCC] on linux2
Type "help", "copyright", "credits" or "license" for more information.
>>> from test import token
>>> word = 'aa'
>>> t = token(word)
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
Boost.Python.ArgumentError: Python argument types in
token.__init__(token, str)
did not match C++ signature:
__init__(_object*, std::string {lvalue})
__init__(_object*, std::string {lvalue}, std::vector<std::string, std::allocator<std::string> > {lvalue})
__init__(_object*, std::string {lvalue}, std::vector<std::string, std::allocator<std::string> > {lvalue}, std::vector<std::string, std::allocator<std::string> > {lvalue})
__init__(_object*)
>>>
Run Code Online (Sandbox Code Playgroud)
有人能指出我的来源吗?不应该Boost.Python负责将python转换str为std::string?
涉及的软件/库的版本:
Boost version: 1.46.1
Python version: 2.7.2
Compiler: g++ (SUSE Linux) 4.6.2
Makefile generation: cmake version 2.8.6
Make: GNU Make 3.82
Run Code Online (Sandbox Code Playgroud)
eca*_*mur 10
您的参数类型是std::string &可修改的引用std::string.Python字符串是不可变的,因此无法转换为可修改的引用.你的参数类型应该是const std::string &.