【问题标题】:How to use macro in lua如何在lua中使用宏
【发布时间】: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,它成功了。

更多这种方式只适用于一个函数,谁能告诉我宏有什么问题?即使使用整数值,该消息也会出现。

【问题讨论】:

  • 看看我的答案的更新版本。我在那里修复了字符串常量问题(在一个后续问题之后......看起来很像这个问题的可怕)被询问和回答。
  • 非常感谢,以前没见过。

标签: c lua lua-api


【解决方案1】:

使用字符串字面量没有意义:

GET_INTEGER_WARN(-1, "wooxy_value"); 

结果

p->"wooxy_value" = lua_tointeger(L, -1);

去掉引号:

GET_INTEGER_WARN(-1, wooxy_value);

【讨论】:

    猜你喜欢
    • 2021-05-31
    • 2015-04-06
    • 1970-01-01
    • 2020-12-20
    • 2014-05-20
    • 1970-01-01
    • 2017-05-14
    • 2014-08-24
    • 1970-01-01
    相关资源
    最近更新 更多