[Date Prev][Date Next][Thread Prev][Thread Next]
[Date Index]
[Thread Index]
- Subject: attempt to concatenate a userdata value
- From: Kuang Chen <kchenphy@...>
- Date: Sun, 31 Jan 2010 11:14:22 -0800
Hi,
I am encountering this error "attempt to concatenate a userdata
value" when I am trying to write a tostring metamethod for userdata
"realvector". This is what the function looks like:
static int realvector_tostring(lua_State* L)
{
lua_pushfstring(L, "realvector: %p", realvector_check(L,1));
return 1;
}
and realvector_check verifies that the argument is realvector by
verifying its metatable.
realvector *realvector_check(lua_State *L, int index)
{
void *p = luaL_checkudata(L, index, "realvector_meta");
luaL_argcheck(L, p != NULL, index, "realvector expected");
return (realvector *)p;
}
the definition of realvector is
typedef struct
{
double val[3];
}realvector;
I am pretty sure that realvector_tostring is called when print() or
tostring() is called in lua. What is weired is everything would work
OK if the address of the object is not included in the format string
of lua_pushfstring. For instance, it would print " realvector: 1 2 3
"if I were to change my code to
realvector* p = realvector_check(L,1)
lua_pushfstring(L, "realvector: %f %f %f", p->val[0], p->val[1], p->val[2]);
assuming the content of realvector is 1 2 3. I searched the archive
for a while and didn't find much. So I am wondering if I could get any
helpful suggestion here.
Thanks,
Kuang