简体   繁体   中英

boost.python No Python class registered for C++ class std::string

When running a c++ class member function from python, i get this error: No Python class registered for C++ class std::string The member function is:

class holiday_calendar {
public:
  const std::string& get_name() const;
  void set_name(const std::string&);
};

I have used this exposing code

class_<holiday_calendar>("holiday_calendar")
.def("getname", &holiday_calendar::get_name, return_internal_reference<>())
.def("setname", &holiday_calendar::set_name);

I can instantiate the object holiday_calendar, I can call setname('SOMENAME') on it but calling getname() fails with the above error

Your method get_name returns a const std::string& , try this:

class_<holiday_calendar>("holiday_calendar")
.def("getname", &holiday_calendar::get_name, return_value_policy<copy_const_reference>())
.def("setname", &holiday_calendar::set_name);

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM