1

I have this function in C++11:

bool ccc(const string cc) {
    
vector<string> digits;
    
int aux;
    
for(int n = 0; n < cc.length(); ++n) {
    
digits.push_back(to_string(cc[n])); }
    
for(int s = 1; s < digits.size(); s += 2) {
    
aux = stoi(digits[s]);
    
aux *= 2;
    
digits[s] = to_string(aux);
    
aux = 0;
    
for(int f = 0; f < digits[s].length(); ++f) {
    
aux += stoi(digits[s][f]); }
    
digits[s] = to_string(aux);
    
aux = 0; }
    
for(int b = 0; b < digits.size(); ++b) {
    
aux += stoi(digits[b]); }
    
aux *= 9;
    
aux %= 10;
    
return (aux == 0); }

And I get this error when compiling with g++ with the -std=c++11 flag:

crecarche.cpp: In function ‘bool ccc(std::string)’:
    
crecarche.cpp:18:12: error: no matching function for call to ‘stoi(__gnu_cxx::__alloc_traits<std::allocator<char>, char>::value_type&)’
    
18 | aux += stoi(digits[s][f]); }
        |        ~~~~^~~~~~~~~~~~~~

But I used the stoi function after and I did not get any error with that line.

Why is the compiler throwing me this error and how can I fix it?

mkrieger1
  • 19,194
  • 5
  • 54
  • 65
MxtApps
  • 21
  • 4

1 Answers1

1

The error message is telling you that the argument you pass to stoi is of the type

__gnu_cxx::__alloc_traits<std::allocator<char>, char>::value_type&

which is a fancy way of saying char&. This happens because digits[s] is already of type string&, and subscribing it further gives you a char&.

It's not clear to me what you are trying to accomplish. Maybe you need to remove the extra subscript, or use digits[s][f] - '0' to compute the digit value. C++ requires that the decimal digits are represented by subsequent code points, so this works even in theoretical implementations which are not based on the ISO 646 subset of Unicode.

Florian Weimer
  • 32,022
  • 3
  • 48
  • 92