0

I am currently learning C++ and I am curious if there is a fix to the warning I am getting.

I am using Int32 to define my integers. I receive the following error when trying to set a variable the length of a string utilising the .length() function.

The line of code in question is as follows:

int32 HiddenWordLength = MyHiddenWord.length();

Many thanks!

2
  • It's a warning, not an error. Commented Dec 4, 2016 at 20:28
  • Note: lengths are usually unsigned quantities. The int32_t type is a signed quantity. You may have loss of precision because a signed type usually reserves at least 1 bit for the sign. Think about it, can the length of a word be negative? So, why are you using a signed value for a length? Commented Dec 4, 2016 at 21:32

2 Answers 2

1

MyHiddenWord.length(); is not returning an int32 type, the compiler will silently adapt/convert that into an int32 but is making you aware that you can loss information with that implicit operation.

Sign up to request clarification or add additional context in comments.

Comments

0

string.length() returns a value of type size_t (which is defined as unsigned int). The compiler throws a warning because your Int32 variable might not be able to hold the value returned from the .length() method, as the Int32 type can hold values of the range –2,147,483,648 to 2,147,483,647 whereas the size_t type (unsigned int) can have values from 0 to 4,294,967,295.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.