【发布时间】:2016-02-13 20:48:39
【问题描述】:
正在寻找关于 lua 的几个线程,发现这篇文章很有趣:
Alert messages for lua functions
我正在尝试对我的代码使用相同的宏,并对操作进行一些更改:
#define GET_INTEGER_WARN(ind, fld) do { \
lua_getfield(L, ind, #fld); \
p->##fld = lua_tointeger(L, -1); \
\
if (!lua_isinteger(L, -1)) \
printf(#fld" allows only numbers;"); \
} while (0)
我的代码:
lua_getfield(L, -1, "wooxy_value");
p->wooxy_value = lua_tointeger(L, -1);
lua_getfield(L, -2, "wooxy_type");
p->wooxy_type = lua_tointeger(L, -1);
我按照作者的解释更改了我的代码,因此:
GET_INTEGER_WARN(-1, "wooxy_value");
GET_INTEGER_WARN(-2, "wooxy_type");
更多宏错误出现在以下位置:
p->##fld = lua_tointeger(L, -1); \
编译错误:error c2059 syntax error 'string'
我做了一个测试,将函数p->##fld替换为p->wooxy_value,它成功了。
更多这种方式只适用于一个函数,谁能告诉我宏有什么问题?即使使用整数值,该消息也会出现。
【问题讨论】:
-
看看我的答案的更新版本。我在那里修复了字符串常量问题(在一个后续问题之后......看起来很像这个问题的可怕)被询问和回答。
-
非常感谢,以前没见过。