如何使用 Luabind 绑定重载函数?
我正在用 c++ 编写一个游戏引擎,它将提供 Lua 脚本(我使用 Luabind 进行包装),并且我在绑定重载函数时遇到一些问题。即:我有一个重载函数:
void setGlobalPosition(const Vec3&position);
void setGlobalPosition(Real x, Real y, Real z);
我想公开这两个都是lua。 显然这是错误的:
luabind::module(L)[ luabind::class_
我在这个网站上找到了一种方法 http://www.codeproject.com/KB/graphics/luabindLuaAndOgre3d.aspx?msg=3376320 (非常好的 Luabind 教程 - 我强烈推荐它),如下所示:
luabind::module(L) [ luabind::class_
但它也给了我错误(如果有人感兴趣,我可以附上它们)。
我也尝试过 .def("setGlobalPosition", Critter::Body::setGlobalPosition
但仍然错误。
有什么想法我该怎么做吗?
编辑: 好的,我找到了一种方法:
.def("setGlobalPosition", ( void(Critter::Body::*)(Vector3) ) &Critter::Body::setGlobalPosition )
从 luabind 文档中,但我收到错误(第一个):
错误 C2440:“类型转换”:不能 从“重载函数”转换为 'void (__thiscall Critter::Body::* )(食人魔::Vector3)'
但无论如何,问题的出现是因为这个函数是继承的(它来自 NxOgre::Actor::
所以我不认为这是正确的方法
编辑 2 :
我有只是尝试将函数的版本与 3 个浮点作为参数绑定,并且......令人惊讶的是,一切都编译得很好,但带有 vector3 的版本却没有......:(
这是我用来实现 3 个浮点函数的:
.def("setGlobalPosition", ( void(Critter::Body::*)(float,float,float) ) &Critter::Body::setGlobalPosition )
我被难住了关于这个;(
I am writing a game engine in c++ which will provide Lua scripting ( for which wrapping I am using Luabind ) and I am having some problems to bind overloaded functions. Namely: I have am overloaded function :
void setGlobalPosition(const Vec3& position);
void setGlobalPosition(Real x, Real y, Real z);
And I would like to expose both of these to lua.
obviously this is wrong:
luabind::module(L)[
luabind::class_<Critter::Body>("Body")
.def("setGlobalPosition", &Critter::Body::setGlobalPosition )
];
I have found a way to do it on this site http://www.codeproject.com/KB/graphics/luabindLuaAndOgre3d.aspx?msg=3376320 (very good tutorial for Luabind - I strongly recommend it) like this :
luabind::module(L)[
luabind::class_<Critter::Body>("Body")
.def("setGlobalPosition", (void( Critter::Body::*)(const Vector3&))Critter::Body::setGlobalPosition )
];
but it also gives me errors (I can attach them if somebody is interested).
I have also tried.def("setGlobalPosition", Critter::Body::setGlobalPosition<Vector3> )
but still errors.
Any ideas how can I do it ?
EDIT:
Ok, I have found a way to do it like that:
.def("setGlobalPosition", ( void(Critter::Body::*)(Vector3) ) &Critter::Body::setGlobalPosition )
from the luabind documentation but I get errors (the first one):
error C2440: 'type cast' : cannot
convert from 'overloaded-function' to
'void (__thiscall Critter::Body::*
)(Ogre::Vector3)'
but anyway the problem arises cuz this function is inherited (it comes from NxOgre::Actor::
so I don't that the right approach anyway
EDIT 2 :
I have just tried to bind the version of function with 3 floats as parameters and ... surprisingly everything compiles just fine but the version with vector3 does not.... :(
this is what I have used to implement 3 float function:
.def("setGlobalPosition", ( void(Critter::Body::*)(float,float,float) ) &Critter::Body::setGlobalPosition )
I am stumped about this ;(
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
这个问题现在看起来很老了,但我想因为你仍然没有最新编辑的答案:
在要转换的函数类型中缺少 const 限定符和引用与符号。它必须是:
This question seems quite old now, but I figured since you still don't have the answer to your latest edits:
is missing the const qualifier and reference-ampersand in the function-type to cast to. It's got to be:
正如 DeadMG 所指出的,您在成员函数之前缺少一个 & 符号。您链接的教程也缺少它。有些编译器可能不关心,但 g++ 会关心,其他一些编译器可能也会关心。
As DeadMG pointed out, you are missing an ampersand before the member function. The tutorial you linked is also missing it. Some compilers might not care, but g++ does and probably some others do too.