如何修复隐式转换警告?
How to fix implicit conversion warnings?
我正在阅读C++如何编程第9版,尝试学习c ++,并尝试在第7章中使用骑士问题进行练习。
基于输出中的"64",代码显然存在某种逻辑错误(我将自行修复),但在执行此操作之前,我坚持理解以及如何处理编译器警告。
我将 board 声明为无符号 int 的二维数组,因为它只能包含值 0-7。同样,出于同样的原因,我将 for 循环中的变量声明为无符号 int。
我已经阅读了有关此的堆栈溢出的回复,但它们很快就超出了我的脑海。
#include <iostream>
#include <array>
#include <iomanip>
using namespace std;
void printArray(const array< array< unsigned int, 8 >, 8 >);
int main() {
array< array< unsigned int, 8 >, 8 > board = {};
array< int, 8 > horizontal = {{ 2, 1, -1, -2, -2, -1, 1, 2 }};
array< int, 8 > vertical = {{ -1, -2, -2, -1, 1, 2, 2, 1 }};
unsigned int currentRow = 0;
unsigned int currentCol = 0;
for (unsigned int i = 0; i < 64; i++) {
board[currentRow][currentCol] = i + 1;
for (unsigned int j = 0; j < 8; j++) {
if (currentRow + horizontal[j] >= 0 &&
currentRow + horizontal[j] < 8 &&
currentCol + vertical[j] >= 0 &&
currentCol + vertical[j] < 8 &&
board[currentRow + horizontal[j]][currentCol + vertical[j]] == 0
) {
currentRow = currentRow + horizontal[j];
currentCol = currentCol + vertical[j];
break;
}
}
}
printArray(board);
}
void printArray(const array< array< unsigned int, 8 >, 8 > myBoard) {
for ( unsigned int i = 0; i < 8; i++) {
for ( unsigned int j = 0; j < 8; j++) {
cout << '[' << setw(2) << setfill('0') << myBoard[i][j] << "] ";
}
cout << endl;
}
}
输出:
~/Programming/cpp/knight => make
rm -fr build/*
clang++ -std=c++11 -stdlib=libc++ -Weverything -Wno-c++98-compat src/main.cpp -o build/main -Isrc/
src/main.cpp:18:24: warning: implicit conversion changes signedness: 'value_type' (aka 'int') to 'unsigned int' [-Wsign-conversion]
if (currentRow + horizontal[j] >= 0 &&
~ ^~~~~~~~~~~~~
src/main.cpp:18:38: warning: comparison of unsigned expression >= 0 is always true [-Wtautological-compare]
if (currentRow + horizontal[j] >= 0 &&
~~~~~~~~~~~~~~~~~~~~~~~~~~ ^ ~
src/main.cpp:19:22: warning: implicit conversion changes signedness: 'value_type' (aka 'int') to 'unsigned int' [-Wsign-conversion]
currentRow + horizontal[j] < 8 &&
~ ^~~~~~~~~~~~~
src/main.cpp:20:22: warning: implicit conversion changes signedness: 'value_type' (aka 'int') to 'unsigned int' [-Wsign-conversion]
currentCol + vertical[j] >= 0 &&
~ ^~~~~~~~~~~
src/main.cpp:20:34: warning: comparison of unsigned expression >= 0 is always true [-Wtautological-compare]
currentCol + vertical[j] >= 0 &&
~~~~~~~~~~~~~~~~~~~~~~~~ ^ ~
src/main.cpp:21:22: warning: implicit conversion changes signedness: 'value_type' (aka 'int') to 'unsigned int' [-Wsign-conversion]
currentCol + vertical[j] < 8 &&
~ ^~~~~~~~~~~
src/main.cpp:22:28: warning: implicit conversion changes signedness: 'value_type' (aka 'int') to 'unsigned int' [-Wsign-conversion]
board[currentRow + horizontal[j]][currentCol + vertical[j]] == 0
~ ^~~~~~~~~~~~~
src/main.cpp:22:56: warning: implicit conversion changes signedness: 'value_type' (aka 'int') to 'unsigned int' [-Wsign-conversion]
board[currentRow + horizontal[j]][currentCol + vertical[j]] == 0
~ ^~~~~~~~~~~
src/main.cpp:24:35: warning: implicit conversion changes signedness: 'value_type' (aka 'int') to 'unsigned int' [-Wsign-conversion]
currentRow = currentRow + horizontal[j];
~ ^~~~~~~~~~~~~
src/main.cpp:25:35: warning: implicit conversion changes signedness: 'value_type' (aka 'int') to 'unsigned int' [-Wsign-conversion]
currentCol = currentCol + vertical[j];
~ ^~~~~~~~~~~
10 warnings generated.
./build/main
[01] [24] [09] [00] [00] [26] [37] [00]
[10] [41] [02] [25] [36] [00] [00] [27]
[23] [08] [11] [40] [21] [38] [00] [00]
[64] [03] [22] [35] [00] [00] [28] [00]
[07] [12] [05] [20] [39] [34] [00] [00]
[04] [17] [14] [31] [00] [29] [00] [00]
[13] [06] [19] [16] [33] [00] [00] [00]
[18] [15] [32] [00] [30] [00] [00] [00]
编译器对你说,当你写的时候
currentRow + horizontal[j] >= 0
你添加一个带有unsigned int
的signed int
,所以signed int
被转换成一个unsigned int
,所以总和的结果是一个unsigned int
。unsigned int
始终大于或等于零。
所以这个测试总是true
这可能不完全是你想要的。
同样的问题
currentCol + vertical[j] >= 0
更一般地说,对于signed
/unsigned
值的每一和。
一种选择是将currentRow
和currentCol
定义为(signed
)int
。
否则,您可以在将值添加到signed
值时适当地转换unsigned
int{currentRow} + horizontal[j] >= 0
相关文章:
- 获取隐式转换溢出从无符号到已签名的警告
- 奇怪的缩小转换在 g++ 编译器中加倍到浮点警告
- 隐式重新解释引用时强制转换,没有警告/错误
- 将无符号转换为复杂<int>原因符号转换警告
- 如何解决隐式转换丢失整数精度:'size_t'(又名"无符号长")到'int'警告?
- MSVC 中从 _Ty 警告到 int 警告的转换累积
- 为什么在 c++ 中索引字符串会发出隐式转换警告?
- 从 int 中剥离位时,编译器会警告一个转换,但不警告其他转换.有解决方法吗?
- C++:禁用隐式转换警告
- 为什么 gcc 警告只针对统一初始化缩小转换范围?
- 警告 C4267"参数":从"size_t"转换为"DWORD&quo
- 隐式转换:以下警告是否有效?
- 意外 (IMO) 常量转换警告
- C++:禁用 CMake 中的旧样式转换警告
- 键入从 DWORD 到 64 位指针的强制转换警告
- 警告 C4267"正在初始化":从'size_t'转换为"DWORD",可能会丢失数据
- isspace 函数的性能警告,从 int 转换为布尔值
- 隐式转换警告 int 到 int-looklike
- 编译器警告转换
- 将警告转换为错误