python vector string转换_Boost.Python.ArgumentError:python str未转换为std :: string

I get an error I can't make sense of trying to wrap a rather simple c++ class via Boost.Python.

First, the class:

#include

#include

#include

#include

class token {

public:

typedef boost::shared_ptr ptr_type;

static std::vector<:string> empty_context;

static std::string empty_center;

token(std::string& t = empty_center,

std::vector<:string>& left = empty_context,

std::vector<:string>& right = empty_context)

: center(t), left_context(left), right_context(right) {}

virtual ~token(void) {}

std::vector<:string> left_context;

std::vector<:string> right_context;

std::string center;

};

std::string token::empty_center;

std::vector<:string> token::empty_context;

exposed to python via

BOOST_PYTHON_MODULE(test) {

namespace bp = boost::python;

bp::class_("token")

.def(bp::init<:string bp::optional>&,

std::vector<:string>&> >())

;

}

then, when trying to use it in 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 "", line 1, in

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<:string std::allocator> > {lvalue})

__init__(_object*, std::string {lvalue}, std::vector<:string std::allocator> > {lvalue}, std::vector<:string std::allocator> > {lvalue})

__init__(_object*)

>>>

Can anyone point me to where this comes from? Shouldn't Boost.Python take care of converting python's str to std::string?

Versions of software/libraries involved:

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

解决方案

Your parameter type is std::string &, a modifiable reference to std::string. Python strings are immutable, so can't be converted to a modifiable reference. Your parameter type should be const std::string &.

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值