简体   繁体   English

C++ 错误 - 警告:有符号和无符号整数表达式之间的比较 [-Wsign-compare]

[英]C++ Error - warning: comparison between signed and unsigned integer expressions [-Wsign-compare]

I'm trying to implement a sorting program of numbers, but am getting the following error:我正在尝试实现数字排序程序,但出现以下错误:

错误

I was able to compile and work the code on an online c++ compiler, but when I must run it via the terminal so after attempting to do so, it won't compile anymore.我能够在在线 C++ 编译器上编译和运行代码,但是当我必须通过终端运行它时,在尝试这样做之后,它将不再编译。 There's actually nothing wrong with the code itself, since they're just warnings.代码本身实际上没有任何问题,因为它们只是警告。 But I would like to know how to fix them all, please!但我想知道如何修复它们,拜托!

The errors come from these parts of my code:错误来自我代码的这些部分:

void sort()
{

    int i, j;

    for (i = 0; i < storage.size()-1; i++)
    {

        for (j = 0; j < storage.size()-i-1; j++)
        {
            if (storage[j] > storage[j+1]) // swap the values
            {
                int temp = storage[j];
                storage[j] = storage[j+1];
                storage[j+1] = temp;
            }
        }
    }
}

void print()
{
    
    for(int i = 0; i < storage.size(); i++)
        cout<<storage[i]<<" ";
        cout<<endl;
    }
    
    vector<int> getList()
    {
        return storage;
    }
    
    void setList(vector<int> list)
    {
        storage = list;
    }
};

   void quickSort(int start, int end)

   {

   if (start < end)
   {
       int part = partition(start, end);
       quickSort(start, part - 1);
       quickSort(start + 1, end);
   }
   }

   void sort()
   {
       quickSort(0, storage.size() - 1);
   }

   void print()
   {
for(int i = 0; i < storage.size(); i++)
cout<<storage[i]<<" ";
cout<<endl;
   }

Any help will be much appreciated.任何帮助都感激不尽。 Thank you!谢谢!

would like to know how to fix them all, please!想知道如何修复它们,请!

The diagnostic says that you are comparing numbers of different signedness.诊断表明您正在比较不同符号的数量。 You can fix that by not comparing numbers of different signedness.您可以通过不比较不同符号的数量来解决这个问题。 You can go with using signed type on both sides or unsigned type on both sides.您可以在两侧使用有符号类型或在两侧使用无符号类型。

Aside from the warning, storage.size()-1 is a dangerous operation unless you first enforce that the container is non-empty.除了警告之外, storage.size()-1是一个危险的操作,除非您首先强制容器为非空。 If you subtract from a unsigned zero, you get a very large unsigned value.如果你从一个无符号零中减去,你会得到一个非常大的无符号值。 That is not bad by itself, but when you eventually use that large value as subscript, you get undefined behaviour.这本身并不坏,但是当您最终使用那个大值作为下标时,您会得到未定义的行为。 Simple solution to this is to add +1 to both sides of the comparison: i+1 < storage.size() .对此的简单解决方案是在比较的两侧添加 +1: i+1 < storage.size()

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 警告:有符号和无符号整数表达式之间的比较 [-Wsign-compare] - warning: comparison between signed and unsigned integer expressions [-Wsign-compare] 警告:有符号和无符号整数表达式之间的比较 [-Wsign-compare] - warning: comparison between signed and unsigned integer expressions [-Wsign-compare] 我的 For 循环有什么问题? 我收到警告:有符号和无符号整数表达式之间的比较 [-Wsign-compare] - What is wrong with my For loops? i get warnings: comparison between signed and unsigned integer expressions [-Wsign-compare] C ++编译错误(有符号和无符号整数表达式之间的比较) - C++ Compile Error (comparison between signed and unsigned integer expressions) 警告 - 有符号和无符号整数表达式之间的比较 - A warning - comparison between signed and unsigned integer expressions C ++有符号和无符号整数表达式之间的比较 - C++ Comparison between signed and unsigned integer expressions 在进行make install时,有符号和无符号整数表达式之间的比较警告? - comparison between signed and unsigned integer expressions warning while make install? 警告:有符号和无符号整数表达式之间的比较..如何解决? - warning: comparison between signed and unsigned integer expressions..how to solve it? -gsign-compare警告用g ++表示 - -Wsign-compare warning in g++ 有符号和无符号整数表达式S之间的比较 - Comparison between signed and unsigned integer expressions S
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM