C++ 如何使用LuaBind将std::map绑定到Lua

C++ 如何使用LuaBind将std::map绑定到Lua,c++,stl,lua,luabind,C++,Stl,Lua,Luabind,我试图将我的std::map作为类属性公开给Lua。我已经为getter和setter设置了此方法: luabind::object FakeScript::GetSetProperties() { luabind::object table = luabind::newtable(L); luabind::object metatable = luabind::newtable(L); metatable["__index"] = &this->GetM

我试图将我的
std::map
作为类属性公开给Lua。我已经为getter和setter设置了此方法:

luabind::object FakeScript::GetSetProperties()
{
    luabind::object table = luabind::newtable(L);
    luabind::object metatable = luabind::newtable(L);

    metatable["__index"] = &this->GetMeta;
    metatable["__newindex"] = &this->SetMeta;

    luabind::setmetatable<luabind::object, luabind::object>(table, metatable);

    return table;
}
<>但是,我在C++中提供的代码没有编译。它告诉我,在这一行
metatable[“\uu index”]=&this->GetMeta;有一个对重载函数的不明确调用及其后的行。我不确定我做得是否正确

错误消息:

error C2668: 'luabind::detail::check_const_pointer' : 
ambiguous call to overloaded function
c:\libraries\luabind-0.9.1\references\luabind\include\luabind\detail\instance_holder.hpp    75
这些是
FakeScript
中的
SetMeta
GetMeta

static void GetMeta();
static void SetMeta();
之前我是为getter方法做这件事的:

luabind::object FakeScript::getProp()
{
    luabind::object obj = luabind::newtable(L);

    for(auto i = this->properties.begin(); i != this->properties.end(); i++)
    {
        obj[i->first] = i->second;
    }

    return obj;
}
这很好,但它不允许我使用setter方法。例如:

player.scripts["movement"].properties["stat"] = "idle"
print(player.scripts["movement"].properties["stat"])
在这段代码中,它将在两行中触发getter方法。虽然如果它允许我使用setter,我将无法从属性中获取密钥,而属性就是
[“stat”]

这里有关于卢阿宾德的专家吗?我看到大多数人说他们以前从未使用过它。

您需要使用(未记录的)
make\u function()
从函数中生成对象

metatable["__index"] = luabind::make_function(L, &this->GetMeta);
metatable["__newindex"] = luabind::make_function(L, &this->GetMeta);

不幸的是,
make_函数
的这个(最简单的)重载被破坏了,但是您只需要将它作为
make_函数的第二个参数。请在您的问题中,将完整的错误消息按您所看到的那样放进去。@greatwolf我已经放进去了。错误应该会显示候选函数是什么。这就是我得到的全部。实际上,我需要有人告诉我如何以LuaBind方式为元表字段分配函数。请看,我可以将此更改为使用函数而不是属性来访问我的映射,但我关心用户的编程风格。
FakeScript::GetMeta
FakeScript::SetMeta
看起来像什么?这些不是你要绑定的函数,它们是类方法。实际上,在我来这里之前,我已经试过了。我无法让它工作,因为超载的腐败,所以我把它扔掉了。现在你一路帮助我!我打算试试,但我想出了另一种方法来使用函数重载来满足我的需要。
metatable["__index"] = luabind::make_function(L, &this->GetMeta);
metatable["__newindex"] = luabind::make_function(L, &this->GetMeta);