过载功能和luabind的问题

时间:2011-06-27 16:14:41

标签: c++ lua luabind

我有一个我要绑定到lua的类。减少的代码可能是:

class CSprite2D{
    void setPosition(glm::ivec2 val) { m_position = val; }
    void setPosition(int posX, int posY) { m_position.x = posX; m_position.y = posY; }
    static void exposeAPI(lua_State* L,const unsigned char* data)
    {
        assert(L);
        luabind::module(L)[
        luabind::class_<CSprite2D>("CSprite2D")
        .def("setPosition",(void(*)(int,int))&CSprite2D::setPosition)
    ];
    }

当我尝试编译时,编译器抛出错误:

Error   1   error C2440: 'type cast' : cannot convert from 'overloaded-function' to 'void (__cdecl *)(int,int)' c:\directo\gameprojects2008\zeleste2d\src\graphics\gpx\sprite2d.cpp 74
Error   2   error C2780: 'luabind::class_<T> &luabind::class_<T>::def(luabind::detail::operator_<Derived>)' : expects 1 arguments - 2 provided  c:\directo\gameprojects2008\zeleste2d\src\graphics\gpx\sprite2d.cpp 74
Error   3   error C2784: 'luabind::class_<T> &luabind::class_<T>::def(luabind::detail::operator_<Derived>,const Policies &)' : could not deduce template argument for 'luabind::detail::operator_<Derived>' from 'const char [12]'  c:\directo\gameprojects2008\zeleste2d\src\graphics\gpx\sprite2d.cpp 74
Error   4   error C2784: 'luabind::class_<T> &luabind::class_<T>::def(luabind::constructor<A0,A1,A2,A3,A4,A5,A6,A7,A8,A9>,const Policies &)' : could not deduce template argument for 'luabind::constructor<A0,A1,A2,A3,A4,A5,A6,A7,A8,A9>' from 'const char [12]'  c:\directo\gameprojects2008\zeleste2d\src\graphics\gpx\sprite2d.cpp 74
Error   5   error C2780: 'luabind::class_<T> &luabind::class_<T>::def(luabind::constructor<A0,A1,A2,A3,A4,A5,A6,A7,A8,A9>)' : expects 1 arguments - 2 provided  c:\directo\gameprojects2008\zeleste2d\src\graphics\gpx\sprite2d.cpp 74
Error   6   error C2780: 'luabind::class_<T> &luabind::class_<T>::def(const char *,F,Default,const Policies &)' : expects 4 arguments - 2 provided  c:\directo\gameprojects2008\zeleste2d\src\graphics\gpx\sprite2d.cpp 74
Error   7   error C2780: 'luabind::class_<T> &luabind::class_<T>::def(const char *,F,DefaultOrPolicies)' : expects 3 arguments - 2 provided c:\directo\gameprojects2008\zeleste2d\src\graphics\gpx\sprite2d.cpp 74

我知道问题的原因是luabind无法推断出最佳的重载函数,但reading the luabind documentation我正在luabind中正确定义函数。

有什么想法吗?

提前致谢

1 个答案:

答案 0 :(得分:5)

您正在将函数转换为错误的类型,您需要强制转换为指向成员的函数。

尝试:

.def("setPosition", (void (CSprite2D::*)(int, int))&CSprite2D::setPosition)