Preprocessor “invalid integer constant expression” comparing int to double

心已入冬 提交于 2021-02-05 04:51:04

问题


Somewhere in my code, I have preprocessor definition

#define ZOOM_FACTOR 1

In another place I have

#ifdef ZOOM_FACTOR
#if (ZOOM_FACTOR == 1)
#define FONT_SIZE 8
#else
#define FONT_SIZE 12
#endif
#else
#define FONT_SIZE 8
#endif

The problem is when I change ZOOM_FACTOR value to floating point value, for example 1.5, I'm getting compile error C1017: invalid integer constant expression.

Does anyone know why am I getting this error and is there any way to make a comparison between integer and floating point number within preprocessor directive?


回答1:


The error is because the language does not permit it.

As per the C++ standard, [cpp.cond]/1:

The expression that controls conditional inclusion shall be an integral constant expression.

Instead of defining ZOOM_FACTOR as floating point value 1.5, why not define it as a multiple of such value. For example, multiply with a constant such as 2 and then make your comparisons.



来源:https://stackoverflow.com/questions/56144648/preprocessor-invalid-integer-constant-expression-comparing-int-to-double

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!