如何使用 Luabind 绑定重载函数?

发布于 2024-11-03 16:18:48 字数 1772 浏览 1 评论 0原文

我正在用 c++ 编写一个游戏引擎,它将提供 Lua 脚本(我使用 Luabind 进行包装),并且我在绑定重载函数时遇到一些问题。即:我有一个重载函数:

void setGlobalPosition(const Vec3&position);

void setGlobalPosition(Real x, Real y, Real z);

我想公开这两个都是lua。 显然这是错误的:

luabind::module(L)[ luabind::class_("Body") .def("setGlobalPosition", &Critter::Body::setGlobalPosition ) ];

我在这个网站上找到了一种方法 http://www.codeproject.com/KB/graphics/luabindLuaAndOgre3d.aspx?msg=3376320 (非常好的 Luabind 教程 - 我强烈推荐它),如下所示:

luabind::module(L) [ luabind::class_("Body") .def("setGlobalPosition", (void( Critter::Body::*)(const Vector3&))Critter::Body::setGlobalPosition ) ];

但它也给了我错误(如果有人感兴趣,我可以附上它们)。

我也尝试过 .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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

忆离笙 2024-11-10 16:18:48

这个问题现在看起来很老了,但我想因为你仍然没有最新编辑的答案:

(( void(Critter::Body::*)(Vector3) ) &Critter::Body::setGlobalPosition)

在要转换的函数类型中缺少 const 限定符和引用与符号。它必须是:

(( void(Critter::Body::*)(const Vector3&) ) &Critter::Body::setGlobalPosition)

This question seems quite old now, but I figured since you still don't have the answer to your latest edits:

(( void(Critter::Body::*)(Vector3) ) &Critter::Body::setGlobalPosition)

is missing the const qualifier and reference-ampersand in the function-type to cast to. It's got to be:

(( void(Critter::Body::*)(const Vector3&) ) &Critter::Body::setGlobalPosition)
早茶月光 2024-11-10 16:18:48

正如 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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文