Skip to main content
GameDev.net gamedev.net
🔒 Locked

Boost Python - Passing reference argument problem

Started by nbertoa Feb 10, 2011 at 5:00 PM 0 replies 3.8k views
Original Post
nbertoa
nbertoa
Hi community. I am having problem with reference parameters between C++ and Python. I will show you the code and then I will explain the problem:


// C++ class
class Example
{

public:

void SetHello(std::string& str)
{

str = "hello";

}

};


// Boost module
BOOST_PYTHON_MODULE(myModule_ext)
{

class_<Example> ("Example")
.def("SetHello", &Example::SetHello)
;

}




When I execute this code in the python console I get an error.

>> import myModule_ext
>> c = myModule_ext.Example()
>> str = ""
>> c.SetHello(str)
Boost.Python.ArgumentError: Python argument types in
c.SetHello(Example, str)
did not match C++ signature:
SetHello(class Example {lvalue}, class std::basic_string<char,struct std::char_traits<char>,class std::allocator<char> > )



Why can I use str like an std::string&? What can I use in place?
SiCrane
SiCrane
A python string isn't a std::string; it's a string type, but not the same string type. You can no more bind a python string to a std::string & than you could a const char *. In this case I'd just return the string by value rather than have it modify the reference.

Topic Locked

This topic has been locked by a moderator. New replies are not allowed.

Sign in to reply to this topic.