Boost.Python.ArgumentError: строка Python не преобразована в std :: string

Я получаю сообщение об ошибке, не имея смысла пытаться обернуть довольно простой класс C ++ с помощью Boost.Python.
Во-первых, класс:

#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;

подвергается воздействию Python через

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>&> >())
;
}

затем, при попытке использовать его в 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*)
>>>

Кто-нибудь может указать мне, откуда это? 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

3

Решение

Ваш тип параметра std::string &, изменяемая ссылка на std::string, Строки Python являются неизменяемыми, поэтому не могут быть преобразованы в изменяемую ссылку. Ваш тип параметра должен быть const std::string &,

8

Другие решения

Других решений пока нет …