c++ - 将 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
解决方案
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.
推荐阅读
- c# - XML 反序列化为已知类型,其中一个元素的结构是可变的(在 SOAP 服务参考中)
- python - 使用 dask-jobqueue 时,SLURM 批处理文件仍需要 #SBATCH 调用资源分配吗?
- angular - 来自 Json 的 Angular 中的动态路由
- javascript - 使用 jQuery 通过 AJAX POST 传递数组
- apache-spark - 错误:无法在 Spark+Mesos 集群上加载类
- python - 尝试使用 Python 删除文件,但权限错误表明它们正被另一个进程使用
- python - 如何获得Python中每一行的下n行的平均值
- angular - 有没有办法为 Angular 内存泄漏执行静态代码分析/检查?
- angular - AngularFireAuth - 获取 OAuth AccessToken 并持久化
- python - 在 Numpy 数组中实现“更改为其他数字”的有效方法