c++ - 错误 : pasting "tmp_UINT" and "+" does not give a valid preprocessing token

标签 c++ c linux gcc

我有一个宏定义为:

#define GET_GENERIC_VAL(val_type, fmt_printf, prefix, suffix) \
val_type tmp_ ## val_type; \
rtc = (*(INOBJECT**)pObj)->getGeneric(meth, &(tmp_ ## val_type)); \
if (rtc == -1) { \
    TRACE("Error: unable to get method for " #val_type " attribute " << attribute << " of object " << name); \
    return -1; \
} \
sprintf(valueStr, fmt_printf, (prefix)(tmp_ ## val_type ## suffix));

并在 switch case 中调用这个宏:

switch (var_type) {
    case 'u' : { GET_GENERIC_VAL(UINT      ,"%lu",UINT        , + 0        ); break; }
    case 'i' : { GET_GENERIC_VAL(INT       ,"%ld",INT         , + 0        ); break; }
    case 's' : { GET_GENERIC_VAL(STRING    ,"%s" ,const char *, + '\0'     ); break; }
    case 'n' : { GET_GENERIC_VAL(NUMBER    ,"%s" ,const char *, .toString()); break; }
    case 'b' : { GET_GENERIC_VAL(BYTESTRING,"%s" ,const char *, .toString()); break; }
    case 'd' : { GET_GENERIC_VAL(DATE      ,"%s" ,const char *, .toString()); break; }
    case 't' : { GET_GENERIC_VAL(TIME      ,"%s" ,const char *, .toString()); break; }
    default : {
        TRACE("Unknown type of attribute : type '" << var_type << "' for attribute '" << attribute << "' of object " << name << ".");
        return -1;
    }
}

我试图在 gcc 编译器上编译它,但编译器给出了以下错误:

error: pasting "tmp_UINT" and "+" does not give a valid preprocessing token         
error: pasting "tmp_INT" and "+" does not give a valid preprocessing token          
error: pasting "tmp_STRING" and "+" does not give a valid preprocessing token       
error: pasting "tmp_NUMBER" and "." does not give a valid preprocessing token       
error: pasting "tmp_BYTESTRING" and "." does not give a valid preprocessing token   
error: pasting "tmp_DATE" and "." does not give a valid preprocessing token         
error: pasting "tmp_TIME" and "." does not give a valid                             

谁能帮忙解决这个问题? 在SUN平台上编译时运行良好。但是 Linux 出错。

最佳答案

你的问题是标记化错误:

sprintf(valueStr, fmt_printf, (prefix)(tmp_ ## val_type ## suffix));

从您使用宏的方式来看,您不想制作一个预处理器标记,而只想添加后缀。

试试这个:

sprintf(valueStr, fmt_printf, (prefix)(tmp_ ## val_type  suffix));

关于c++ - 错误 : pasting "tmp_UINT" and "+" does not give a valid preprocessing token,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/50405614/

相关文章:

c++ - 使用什么代替 Goto 语句?

c - 这两个操作有区别吗?

linux - 为什么ubuntu上的/dev/random生成数据比debian慢?

php - 如何在 php(ubuntu) 上模拟 on_script_close,就像 .net on_form_close 行为一样?

c++ - 如何将 `swap` 专门用于具有位域成员的类?

c++ - 如何将参数从c++传递到linux shell

c - 查找不能表示为 IEEE-754 32 位 float 的最小整数

linux - 如何使 Cmake 全局可用

c++ - 指向在 C++ 中自动返回 'type' 的模板成员的指针?

似乎无法在c中的链接列表中获得打印功能