警告 - 有符号和无符号整数表达式之间的比较 [英] A warning - comparison between signed and unsigned integer expressions

查看:2441
本文介绍了警告 - 有符号和无符号整数表达式之间的比较的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我目前正在使用加速C ++ ,在练习2-3中遇到了一个问题。

I am currently working through Accelerated C++ and have come across an issue in exercise 2-3.

的程序 - 程序基本上是一个名字,然后在一个星号框架内显示一个问候 - 即你好!包围由*的。

A quick overview of the program - the program basically takes a name, then displays a greeting within a frame of asterisks - i.e. Hello ! surrounded framed by *'s.

练习 - 在示例程序中,作者使用 const int 在问候和星号之间的填充(空格)。他们然后请读者,作为练习的一部分,要求用户输入他们想要的填充有多大。

The exercise - In the example program, the authors use const int to determine the padding (blank spaces) between the greeting and the asterisks. They then ask the reader, as part of the exercise, to ask the user for input as to how big they want the padding to be.

这看起来很容易,我去问用户两个整数( int )并存储它们,并更改程序使用这些整数,删除作者使用的,当编译时,我得到以下警告:

All this seems easy enough, I go ahead ask the user for two integers (int) and store them and change the program to use these integers, removing the ones used by the author, when compiling though I get the following warning;


Exercise2-3.cpp:46:警告:有符号和无符号整数表达式之间的比较

Exercise2-3.cpp:46: warning: comparison between signed and unsigned integer expressions

经过一番研究,似乎是因为代码试图比较上述整数之一( int )到 string :: size_type ,这很好。但我想知道 - 这是否意味着我应该更改一个整数到 unsigned int ?明确地说明我的整数是有符号还是无符号很重要?

After some research it appears to be because the code attempts to compare one of the above integers (int) to a string::size_type, which is fine. But I was wondering - does this mean I should change one of the integers to unsigned int? Is it important to explicitly state whether my integers are signed or unsigned?

 cout << "Please enter the size of the frame between top and bottom you would like ";
 int padtopbottom;
 cin >> padtopbottom;

 cout << "Please enter size of the frame from each side you would like: ";
 unsigned int padsides; 
 cin >> padsides;

 string::size_type c = 0; // definition of c in the program
 if (r == padtopbottom + 1 && c == padsides + 1) { // where the error occurs

以上是代码的相关位, c string :: size_type 因为我们不知道问候可能有多长 - 但是为什么我得到这个问题现在,当作者的代码没有得到使用 const时的问题int

Above are the relevant bits of code, the c is of type string::size_type because we do not know how long the greeting might be - but why do I get this problem now, when the author's code didn't get the problem when using const int? In addition - to anyone who may have completed Accelerated C++ - will this be explained later in the book?

我在使用g ++的Linux Mint上使用g ++来完成加速C ++ 通过Geany,如果这有助于或有所作为(如我所知,它可以在确定 string :: size_type 是)。

I am on Linux Mint using g++ via Geany, if that helps or makes a difference (as I read that it could when determining what string::size_type is).

推荐答案

通常最好将变量声明为 unsigned size_t 如果将它们与大小进行比较,以避免此问题。如果可能,请使用您将要比较的确切类型(例如,与字符串长度比较时使用 std :: string :: size_t )。

It is usually a good idea to declare variables as unsigned or size_t if they will be compared to sizes, to avoid this issue. Whenever possible, use the exact type you will be comparing against (for example, use std::string::size_t when comparing with a string length).

编译器给出了有关比较有符号和无符号类型的警告,因为有符号和无符号整型的范围是不同的,当它们相互比较时,结果可能会令人惊讶。如果你必须进行这样的比较,你应该显式转换其中一个值为一个兼容的类型,也许在检查以确保转换是有效的。例如:

Compilers give warnings about comparing signed and unsigned types because the ranges of signed and unsigned ints are different, and when they are compared to one another, the results can be surprising. If you have to make such a comparison, you should explicitly convert one of the values to a type compatible with the other, perhaps after checking to ensure that the conversion is valid. For example:

unsigned u = GetSomeUnsignedValue();
int i = GetSomeSignedValue();

if (i >= 0)
{
    // i is nonnegative, so it is safe to compare to unsigned value
    if ((unsigned)i >= u)
        BlahBlahBlah();
    else
        YaddaYaddaYadda();
}
else
{
    HandleNegativeValue();
}

这篇关于警告 - 有符号和无符号整数表达式之间的比较的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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