此处不允许将 'int' 值缩小为 'short'
Narrowing conversion of 'int' value to 'short' is not allowed here
最近我将我的 SourcePro 升级到 2021 版本,在 RCB 设置期间,当它提示我使用 select C++ 方言时,我 select 编辑了唯一可用的选项,即 C++14。设置完成后,我开始编译 Tuxedo 代码,目前在多个文件中遇到以下错误,比如我的 Tuxedo 项目中大约有 200-250 个文件,下面是我的 Solaris OS 和编译器的详细信息。
uname -a:
SunOS nzdrb12z 5.11 11.4.40.107.3 sun4v sparc sun4v 非全局区域
cc -V:
cc: Studio 12.6 Sun C 5.15 SunOS_sparc 2017/05/30
Error:
app/sdasup/home/mhp/source/develop/bc/include/bcIControlBlock.h", line
34: Error: Narrowing conversion of 'int' value to 'short' is not
allowed here.
"/app/sdasup/home/mhp/source/develop/bc/include/bcIPControlBlockTran.h",
line 48: Error: Narrowing conversion of 'int' value to 'short' is not
allowed here.
"/app/sdasup/home/mhp/source/develop/bc/include/bcIPConnectionParam.h",
line 44: Error: Narrowing conversion of 'int' value to 'short' is not
allowed here.
"/app/sdasup/home/mhp/source/develop/bc/include/bcMwServicesGuid.h",
line 15: Error: Narrowing conversion of 'int' value to 'short' is not
allowed here.
"/app/sdasup/home/mhp/source/develop/bc/include/bcMwServicesGuid.h",
line 34: Error: Narrowing conversion of 'int' value to 'short' is not
allowed here.
"/app/sdasup/home/mhp/source/develop/bc/include/bcMwServicesGuid.h",
line 36: Error: Narrowing conversion of 'int' value to 'short' is not
allowed here.
"/app/sdasup/home/mhp/source/develop/bc/include/bcMwServicesGuid.h",
line 41: Error: Narrowing conversion of 'int' value to 'short' is not
allowed here.
"/app/sdasup/home/mhp/source/develop/bc/include/bcMwServicesGuid.h",
line 45: Error: Narrowing conversion of 'int' value to 'short' is not
allowed here.
假设我选择了第一个错误信息文件打开bcIControlBlock.h并且行号34指向下面的代码,
DEFINE_GUID(IID_IAbcIControlBlock, 0xc7645022, 0x93e9, 0x11d1, 0x9d, 0x90, 0x0, 0x0, 0xf6, 0x4e, 0x16, 0x6a);
当搜索宏时,它具有以下定义,
#define DEFINE_GUID(name, l, w1, w2, b1, b2, b3, b4, b5, b6, b7, b8) \
EXTERN_C const GUID name \
= { l, w1, w2, { b1, b2, b3, b4, b5, b6, b7, b8 } }
作为解决方法,我用了以下方法来解决它,
DEFINE_GUID(IID_IAbcIControlBlock, 0xc7645022, (short)0x93e9, 0x11d1, 0x9d, 0x90, 0x0, 0x0, 0xf6, 0x4e, 0x16, 0x6a);
虽然它工作正常,但我对我用来解决问题的方法非常怀疑。然而,更改多个文件似乎很痛苦。
问题:
- 解决方法是否正确?
- 如果是这样,那么如何使在一个地方更改它比更改多个文件更通用?
- 如果不是,最好的解决方法是什么,这样它就不会进一步破坏任何东西?
提前致谢。
理想情况下,所有 GUID
字段都应声明为无符号(参见 this Microsoft documentation 示例)。但是某些语言(例如 Java)不支持无符号整数。所以看起来你的 C++ GUID
结构是由 Java 程序员定义的。
如果您可以更改 GUID
的定义,那就去做吧。如果没有,恐怕您将不得不将 (short)
转换应用于所有出现的 DEFINE_GUID
宏(除非您的编译器有某种方法来禁用此错误,如您的评论中所建议的那样原创post)。 Java 程序员必须一直这样做。
最近我将我的 SourcePro 升级到 2021 版本,在 RCB 设置期间,当它提示我使用 select C++ 方言时,我 select 编辑了唯一可用的选项,即 C++14。设置完成后,我开始编译 Tuxedo 代码,目前在多个文件中遇到以下错误,比如我的 Tuxedo 项目中大约有 200-250 个文件,下面是我的 Solaris OS 和编译器的详细信息。
uname -a:
SunOS nzdrb12z 5.11 11.4.40.107.3 sun4v sparc sun4v 非全局区域
cc -V:
cc: Studio 12.6 Sun C 5.15 SunOS_sparc 2017/05/30
Error: app/sdasup/home/mhp/source/develop/bc/include/bcIControlBlock.h", line 34: Error: Narrowing conversion of 'int' value to 'short' is not allowed here. "/app/sdasup/home/mhp/source/develop/bc/include/bcIPControlBlockTran.h", line 48: Error: Narrowing conversion of 'int' value to 'short' is not allowed here. "/app/sdasup/home/mhp/source/develop/bc/include/bcIPConnectionParam.h", line 44: Error: Narrowing conversion of 'int' value to 'short' is not allowed here. "/app/sdasup/home/mhp/source/develop/bc/include/bcMwServicesGuid.h", line 15: Error: Narrowing conversion of 'int' value to 'short' is not allowed here. "/app/sdasup/home/mhp/source/develop/bc/include/bcMwServicesGuid.h", line 34: Error: Narrowing conversion of 'int' value to 'short' is not allowed here. "/app/sdasup/home/mhp/source/develop/bc/include/bcMwServicesGuid.h", line 36: Error: Narrowing conversion of 'int' value to 'short' is not allowed here. "/app/sdasup/home/mhp/source/develop/bc/include/bcMwServicesGuid.h", line 41: Error: Narrowing conversion of 'int' value to 'short' is not allowed here. "/app/sdasup/home/mhp/source/develop/bc/include/bcMwServicesGuid.h", line 45: Error: Narrowing conversion of 'int' value to 'short' is not allowed here.
假设我选择了第一个错误信息文件打开bcIControlBlock.h并且行号34指向下面的代码,
DEFINE_GUID(IID_IAbcIControlBlock, 0xc7645022, 0x93e9, 0x11d1, 0x9d, 0x90, 0x0, 0x0, 0xf6, 0x4e, 0x16, 0x6a);
当搜索宏时,它具有以下定义,
#define DEFINE_GUID(name, l, w1, w2, b1, b2, b3, b4, b5, b6, b7, b8) \
EXTERN_C const GUID name \
= { l, w1, w2, { b1, b2, b3, b4, b5, b6, b7, b8 } }
作为解决方法,我用了以下方法来解决它,
DEFINE_GUID(IID_IAbcIControlBlock, 0xc7645022, (short)0x93e9, 0x11d1, 0x9d, 0x90, 0x0, 0x0, 0xf6, 0x4e, 0x16, 0x6a);
虽然它工作正常,但我对我用来解决问题的方法非常怀疑。然而,更改多个文件似乎很痛苦。
问题:
- 解决方法是否正确?
- 如果是这样,那么如何使在一个地方更改它比更改多个文件更通用?
- 如果不是,最好的解决方法是什么,这样它就不会进一步破坏任何东西?
提前致谢。
理想情况下,所有 GUID
字段都应声明为无符号(参见 this Microsoft documentation 示例)。但是某些语言(例如 Java)不支持无符号整数。所以看起来你的 C++ GUID
结构是由 Java 程序员定义的。
如果您可以更改 GUID
的定义,那就去做吧。如果没有,恐怕您将不得不将 (short)
转换应用于所有出现的 DEFINE_GUID
宏(除非您的编译器有某种方法来禁用此错误,如您的评论中所建议的那样原创post)。 Java 程序员必须一直这样做。