Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to fix C++ warning of implicit conversion?

I am just getting started in C++.

I am trying to get the first three characters of the string 'str', and compare it to a known string, say, 'knownString'.

To do that, I wrote this line of code:

if (str.substr(start, 3) == knownString)

where 'start' is an integer I declared before. But I keep getting this warning message:

warning: implicit conversion changes signedness: 'int' to 'std::__cxx11::basic_string,** **std::allocator >::size_type' (aka 'unsigned int')

Does anyone knows what I can add or I missed in this statement to fix this?

like image 995
Tina Avatar asked Oct 17 '25 10:10

Tina


1 Answers

You can:

Either 1. make the conversion explicit:

str.substr(static_cast<std::string::size_type>(start), 3)

Or 2. not make a conversion in the first place:

std::string::size_type start;

Or 3. ask compiler to not warn about it:

g++ compilation arguments -Wno-sign-conversion

I recommend option 2.

like image 112
eerorika Avatar answered Oct 20 '25 01:10

eerorika



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!