0
votes

I am stuck with a warning on xcode, when I try to use the function of the tesseract package:

ocr->SetImage(im.data, im.cols, im.rows, 3, im.step);

I get a warning:

Implicit conversion loses integer precision: 'size_t' (aka 'unsigned long') to 'int'

How can I solve the problem?

1
Can you be more precise - is this an error or a warning? - Adrian Mole
It is a warning - user11297709
That is a warning, not an error. And clearly you are passing an unsigned 32bit/64bit value where a signed 32bit value is expected, hence the potential loss of precision. Which input value is the size_t and which parameter is the int? - Remy Lebeau
Just add a static_cast<int>(im.data) (assuming it's im.data that's the 'offending' size_t argument). - Adrian Mole
thanks guys static_cast<int>(im.data) worked!!! - user11297709

1 Answers

1
votes

References: Implicit conversion, What is size_t? and this post.

As documentation says:

Implicit conversions are automatically performed when a value is copied to a compatible type.

In your case, trying int to size_t is also an implicit conversion. The reason why the warning mentions about the precision:

  • size_t is always able to store more numbers than int.
  • While size_t holds always a positive value, int can hold also negatives.

According to these two differences between size_t and int, program assumes that some problems or precision loses can occur in the future.

In your case, you can simply convert your size_t type using static_cast conversion:

static_cast<int>(your_size_t_type_parameter)

Note: Using this method can eliminate your warning but this can cause some loses. For example if your size_t value is very huge.