2012-09-05 2 views
3

오류가 발생합니다. Boost.Python을 통해 오히려 간단한 C++ 클래스를 래핑하려고하는 것이 잘못되었습니다. 파이썬에서 사용하려고 할 때Boost.Python.ArgumentError : python str이 std :: string으로 변환되지 않았습니다.

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

다음

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 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은 파이썬의 strstd::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 

답변

8

귀하의 매개 변수 유형은 std::string &, std::string에 수정 참조입니다. 파이썬 문자열은 변경할 수 없으므로 수정 가능한 참조로 변환 할 수 없습니다. 매개 변수 유형은 const std::string &이어야합니다.

+0

정말 고마워요, 이건 ... 잠시 동안 변환하려고 노력하고 있습니다 ... (그렇게하자 마자이 답을 받아 들일 것입니다) – tjanu

관련 문제