LuaBind 0.9.1 迭代器总是只弹出最后一个对象。

我使用 luabind 从 lua 脚本读取数组时遇到了奇怪的问题。

lua 脚本如下:

root =
{
        id = 1,
        id = 2,
        id = 3
};

c++ 代码如下:

luabind::object data\_root = luabind::globals(L)\["root"\];
for (luabind::iterator i(data_root), end; i != end; ++i)
{
    luabind::object data = *i;
    unsigned int id = luabind::object_cast<unsigned int>(data);
    std::cout << "id:" << id << std::endl;
}

输出结果只有:

id:3

我想输出 [root] 的所有元素,但它只输出最后一个并且超出范围。

谢谢,Jason :)

原文链接 https://stackoverflow.com/questions/6724078

点赞
stackoverflow用户298661
stackoverflow用户298661

将下面翻译成中文并且保留原本的 markdown 格式

There aren't multiple elements of root, it only has one. You assigned the key `id` to 
three different values, but the key only exists once and only has one value associated 
with it, so you basically only ever said `root = { id = 3 }`.

并没有多个名为 root 的元素,只有一个。你给 id 键分配了三个不同的值,但是这个键仅存在一次,并且仅与一个值相关联,因此你基本上只是说 root = { id = 3 }

2011-07-17 13:27:25