简体   繁体   中英

C++ std::string to Ruby VALUE

How can I convert a C++ std::string object into a Ruby VALUE object?

I tried rb_str_new2(c_string) , but it did not work.

I have a function

VALUE foo(){return rb_str_new2(c_string);};

and that gives an error message:

cannot convert ‘std::string {aka std::basic_string<char>}’ to ‘const char*’ for argument ‘1’ to ‘size_t strlen(const char*)’

You are passing std::string to the function, but it expects a null-terminated const char * .

The std::string::c_str() member function can be used to get one:

rb_str_new_cstr(string.c_str());

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