IF comparative test for error values ​​(C ++)

My current project would be too long to post here, however, this is the only line that produces really weird behavior, at least as I see it. I use an object clipto store relatively short lines (the maximum size is used in 35), however, the condition does not work when working with negative values ​​in start.

I tried adding (const int)before clip.length(), but the result did not change:

Any ideas what this means? I am using g ++ on Ubuntu 14.04.

 void Cut ( const int start, const int stop )
 { if (start > clip.length() ) cout << "start: " << start << " > " << clip.length() << endl;

  ...
 }

enter image description here

+4
source share
2 answers

The reason for this comparison:

if (start > clip.length()) {

. , :

if (start > static_cast<int>(clip.length())) {

, ( , ):

test.cpp:8:13: warning: comparison between signed and unsigned integer expressions [-Wsign-compare]

g++ -Wall , , -Wextra.

+4

, length() unsigned int, signed int , .

. . :

[expr]

....

, , , .

+12

All Articles