0

I have the following function:

ubigint::ubigint (const string& that){
           DEBUGF ('~', "that = \"" << that << "\"");
           for (char digit: that) {
              if (not isdigit (digit)) {
                 throw invalid_argument ("ubigint::ubigint(" + that + ")");
              }
           }

           //string thatCopy = that;

           for(string::reverse_iterator rit = that.rbegin(); rit != that.rend(); rit++) // iterate the string from end to start and store it in the vector
            {
                ubig_value.push_back(*rit); // push the character
            }  


        }

and I am getting the following error

 error: no matching function for call to ‘__gnu_cxx::__normal_iterator

I created a copy of the string that in the function and this solved the problem

ubigint::ubigint (const string& that){
   DEBUGF ('~', "that = \"" << that << "\"");
   for (char digit: that) {
      if (not isdigit (digit)) {
         throw invalid_argument ("ubigint::ubigint(" + that + ")");
      }
   }

   string thatCopy = that;

   for(string::reverse_iterator rit = thatCopy.rbegin(); rit != thatCopy.rend(); rit++) // iterate the string from end to start and store it in the vector
    {
        ubig_value.push_back(*rit); // push the character
    }  


}

why does that happen?. Am I not allowed to iterate over constant strings?

itachi_uchiha
  • 81
  • 1
  • 9
  • 1
    Yes, you can iterate over constant stings, but you need a `const_reverse_iterator`, not a `reverse_iterator`. – Pete Becker Jul 02 '17 at 00:54
  • Stop writing loops. All that code is this: `if ( !std::all_of(that.begin(), that.end(), isdigit)) { throw invalid_argument ("ubigint::ubigint(" + that + ")";}` – PaulMcKenzie Jul 02 '17 at 00:55

0 Answers0