尝试将类暴露给Lua时,出现“无法将const转换为&”的C2664错误。

我试图使用LuaBridge将两个类SpriteTexture公开给Lua。这些类像这样:

class Texture
{
public:
    Texture(const std::string& filename);
    ...
}

class Sprite
{
public:
    Sprite(Texture& texture);
    ...
}

现在,我尝试这样将它们绑定到Lua:

lua_State* L = ...;
luabridge::getGlobalNamespace(L)
    .beginClass<Texture>("Texture") // 不公开构造函数,只是类
    .endClass()
    .beginClass<Sprite>("Sprite")
    .addConstructor<void(*)(Texture&)>() // 这会导致错误
    .endClass();

然而,这会产生以下编译错误:

C2664: cannot convert argument 1 from 'const Texture' to 'Texture &'

我为什么会得到这个错误,如何解决它?

点赞