我将宏定义为:
#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的错误。
答案 0 :(得分:2)
你的问题是错误的标记化:
sprintf(valueStr, fmt_printf, (prefix)(tmp_ ## val_type ## suffix));
从你使用宏的方式来看,你不想制作一个预处理器令牌,但你只想添加后缀。
请改为尝试:
sprintf(valueStr, fmt_printf, (prefix)(tmp_ ## val_type suffix));