samedi 2 avril 2016

Why does cleaning istringstream fail?

While converting a string to integer I just fail to clear my istringstream to place in another number. I have tried all kinds of different solutions but it just wont work. There are a few work arounds but I'd like to learn to know why...

So basically this is how my code looks like

#include <iostream>
#include <string>
#include <sstream>

int main() {

  std::string a = "153";
  std::string c = "2556";

  int b;

  std::istringstream convert(a);

  convert >> b;

  std::cout << b << std::endl;

  convert.str( std::string() );
  //convert.str("");
  convert.clear();

  convert(c);

  convert >> b;

  std::cout << b << std::endl;

  return 0;
}

And the following output error

C:\...\string to int.cpp|28|error: no match for call to '(std::istringstream {aka std::basic_istringstream<char>}) (std::string&)'|

Thanks :)

Aucun commentaire:

Enregistrer un commentaire