预处理器“无效的整数常量表达式"被指定为“无效整数常量表达式".比较int和double [英] Preprocessor "invalid integer constant expression" comparing int to double

查看:103
本文介绍了预处理器“无效的整数常量表达式"被指定为“无效整数常量表达式".比较int和double的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在我的代码中的某个地方,我有预处理器定义

Somewhere in my code, I have preprocessor definition

#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 的值更改为浮点的值(例如 1.5 )时,出现编译错误C1017:无效的整数常量表达式.

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.

有人知道我为什么会收到此错误,并且有什么方法可以在预处理指令中比较 integer floating point number 吗?

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?

推荐答案

错误是因为该语言不允许.

The error is because the language does not permit it.

根据C ++标准, [cpp.cond]/1 :

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

控制条件包含的表达式应为整数常量表达式.

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

不是将 ZOOM_FACTOR 定义为浮点值 1.5 ,而是为什么不将其定义为该值的倍数.例如,与常数 2 相乘,然后进行比较.

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.

这篇关于预处理器“无效的整数常量表达式"被指定为“无效整数常量表达式".比较int和double的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆