首页 > 解决方案 > 将 int 与 double 进行比较的预处理器“无效的整数常量表达式”

问题描述

在我的代码的某处,我有预处理器定义

#define ZOOM_FACTOR 1

在另一个地方我有

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

问题是当我将ZOOM_FACTOR值更改为floating point值时,例如1.5,我收到编译错误C1017: invalid integer constant expression

有谁知道我为什么会收到这个错误,有没有办法在预处理器指令之间integer进行比较?floating point number

标签: c++preprocessor

解决方案


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.


推荐阅读