problem binding a c++ function with luabind

Started by
2 comments, last by Gambini 11 years ago

Hello,

I'm pretty new to luabind. I'm having a little problem binding one of my string functions(convert to uppercase). Just couldn't figure it out.

[source]void STools::UCase(char *txt)
{
for (;;)
{
u_int32 t = *reinterpret_cast(txt);
...
txt++;
}
}
[/source]


Here's the luabind code
[source]luabind::module(myLuaState) [
luabind::def("convert_upper", STools::UCase)
]; [/source]


Compiler Error

[source]error C2664: 'void (char *)' : cannot convert parameter 1 from 'const char *' to 'char *' [/source]

Any help is appreciated!

Advertisement

http://www.rasterbar.com/products/luabind/docs.html#out-value

That explains how your C++ functions can't take non-const ref/pointers if you want to bind them to Lua. So, the out_value function policy will add a return value in Lua code, rather than modifying the input.

Where your C++ code would look like

char* str = "hi";
UCase(str);
std::cout << str; //prints 'HI'

Your Lua code would look like

local str = "hi"
local ucstr = UCase(str)
--// or you can have str = UCase(str)
print(ucstr) //prints 'HI'

Thanks a lot Gambini,

I've tried your suggestion but I'm still getting compiler errors. Any other suggestions?

out_value(_1) causes the following error:

[SOURCE] error C2352: 'luabind::native_converter_base<T>::match' : illegal call of non-static member function[/SOURCE]

out_value(_2) causes the following:

[SOURCE]error C2664: 'void (char *)' : cannot convert parameter 1 from 'const char *' to 'char *'[/SOURCE]

It looks like out_value doesn't work well with MSVC (here).

You'll have to either re-think the function or use Lua's "string.upper" function.

Another option is to make your own string class with UCase as a member function. That would bind perfectly fine to Lua, since UCase wouldn't need to take any parameters and only modify the object data.

This topic is closed to new replies.

Advertisement