Author Topic: Size_type Erro  (Read 3052 times)

Offline williampool

  • Single posting newcomer
  • *
  • Posts: 1
Size_type Erro
« on: June 27, 2024, 10:16:17 am »
Hello,
I am new to Code::Blocks (and C++) and have been working through examples from C++ All-In-One for dummies, 4th Edition. The code below and several other codes I have tried to run keep having the following error:

error: conversion to 'std::__cxx11::basic_string<char>::size_type' {aka 'long long unsigned int'} from 'int' may change the sign of the result [-Werror=sign-conversion]|

The code for the warning above can be found below. The build error was referencing the following line: InkLevelPercent = InkLevelPercent - words.length();

In another code I tried to run, I received the following similar error:

error: conversion to 'std::vector<std::__cxx11::basic_string<char> >::size_type' {aka 'long long unsigned int'} from 'int' may change the sign of the result [-Werror=sign-conversion]|

The build error was referencing the following line in that code (I can post that code as well if needed):

int randomIndex = rand() % words.size();

Is there something obvious I may be missing? Browsing through Google I kept seeing references to size_t but wasn't sure if that was applicable, and if so, how to change the code to correct the issue.

Offline Miguel Gimenez

  • Developer
  • Lives here!
  • *****
  • Posts: 1631
Re: Size_type Erro
« Reply #1 on: June 27, 2024, 12:11:24 pm »
Copy of a post in Reddit.

Waiting for the spam link...