我使用Ryan Pavlik的Lua 5.1的主分佈的luabind 0.9.1,Win XP SP3上的cygwin +最新的補丁x86,boost 1.48,gcc 4.3.4。 Lua和boost是cygwin預編譯版本。luabind:無法從非內置類索引表中檢索值
我已經在靜態和共享版本中成功構建了luabind。
兩個版本都通過了test_object_identity.cpp測試的所有測試,其中兩個版本均失敗。
我追查到以下問題: 如果爲NON內置類(即不是int,string等)創建表中的條目,則無法檢索值。
這裏有一個代碼塊演示了此:
#include "test.hpp"
#include <luabind/luabind.hpp>
#include <luabind/detail/debug.hpp>
using namespace luabind;
struct test_param
{
int obj;
};
void test_main(lua_State* L)
{
using namespace luabind;
module(L)
[
class_<test_param>("test_param")
.def_readwrite("obj", &test_param::obj)
];
test_param temp_object;
object tabc = newtable(L);
tabc[1] = 10;
tabc[temp_object] = 30;
TEST_CHECK(tabc[1] == 10); // passes
TEST_CHECK(tabc[temp_object] == 30); // FAILS!!!
}
TABC [1]的確是10,而TABC [temp_object]不是30! (實際上,它似乎是零)
但是,如果我使用迭代來檢查tabc條目,那麼有兩個條目與正確的鍵/值對。
任何想法?
BTW,重載==操作符是這樣的:
#include <luabind/operator.hpp>
struct test_param
{
int obj;
bool operator==(test_param const& rhs) const
{
return obj == rhs.obj;
}
};
和
module(L)
[
class_<test_param>("test_param")
.def_readwrite("obj", &test_param::obj)
.def(const_self == const_self)
];
不改變結果。
我也嘗試從[]運算符切換到settable()和gettable()。結果是一樣的。我可以用調試器看到調用了默認的密鑰轉換,所以我想這個錯誤來自其中的某個地方,但是我無法弄清楚究竟是什麼問題。
如下面的簡單的測試案例顯示,有是肯定的Luabind的轉換爲複雜類型的錯誤:
struct test_param : wrap_base
{
int obj;
bool operator==(test_param const& rhs) const
{ return obj == rhs.obj ; }
};
void test_main(lua_State* L)
{
using namespace luabind;
module(L)
[
class_<test_param>("test_param")
.def(constructor<>())
.def_readwrite("obj", &test_param::obj)
.def(const_self == const_self)
];
object tabc, zzk, zzv;
test_param tp, tp1;
tp.obj = 123456;
// create new table
tabc = newtable(L);
// set tabc[tp] = 5;
// o k v
settable(tabc, tp, 5);
// get access to entry through iterator() API
iterator zzi(tabc);
// get the key object
zzk = zzi.key();
// read back the value through gettable() API
// o k
zzv = gettable(tabc, zzk);
// check the entry has the same value
// irrespective of access method
TEST_CHECK (*zzi == 5 &&
object_cast<int>(zzv) == 5);
// convert key to its REAL type (test_param)
tp1 = object_cast<test_param>(zzk);
// check two keys are the same
TEST_CHECK(tp == tp1);
// read the value back from table using REAL key type
zzv = gettable(tabc, tp1);
// check the value
TEST_CHECK(object_cast<int>(zzv) == 5);
// the previous call FAILS with
// Terminated with exception: "unable to make cast"
// this is because gettable() doesn't return
// a TRUE value, but nil instead
}
希望有人比我聰明可以算出來, THX
我已經將問題追溯到Luabind在每次使用複雜值作爲關鍵字時創建一個NEW DISTINCT對象這一事實(但如果您使用原始對象或對象,則不會這樣做)。
這裏的演示此小測試用例:
struct test_param : wrap_base
{
int obj;
bool operator==(test_param const& rhs) const
{ return obj == rhs.obj ; }
};
void test_main(lua_State* L)
{
using namespace luabind;
module(L)
[
class_<test_param>("test_param")
.def(constructor<>())
.def_readwrite("obj", &test_param::obj)
.def(const_self == const_self)
];
object tabc, zzk, zzv;
test_param tp;
tp.obj = 123456;
tabc = newtable(L);
// o k v
settable(tabc, tp, 5);
iterator zzi(tabc), end;
std::cerr << "value = " << *zzi << "\n";
zzk = zzi.key();
// o k v
settable(tabc, tp, 6);
settable(tabc, zzk, 7);
for (zzi = iterator(tabc); zzi != end; ++zzi)
{
std::cerr << "value = " << *zzi << "\n";
}
}
通知如何TABC [TP]第一具有值5,然後通過按鍵對象訪問時改寫爲7。但是,當通過tp重新訪問時,會創建一個新條目。這就是gettable()隨後失敗的原因。
THX, 大衛
您是否曾經解決過這個問題?使用int值作爲表格的鍵時,我已經遇到了這個問題。 local testTable = {[10] =「green」,[9] =「orange」,[8] =「yellow」} - 如果我使用字符串而不是數字作爲鍵 - 它工作正常 - 我把這個表作爲參數到一個C++函數,我也得到了鑄造錯誤 – Steve 2012-07-08 22:22:02