0

I have the following piece of code:

#include <sstream>
#include <iostream>
using namespace std;

int main()
{
    string inp, s;
    istringstream iss;
    do
    {
        getline (cin, inp);
        iss(inp);
        int a = 0, b = 0; float c = 0;
        iss >> s >> a >> b >> c;
        cout << s << " " << a << " " << b << " " << c << endl;
    }
    while (s != "exit");
}

which generates the following error:

error: no match for call to ‘(std::istringstream) (std::string&)’

I know that the issue may be averted by using istringstream iss(inp); within the loop, however, is it not possible to move this defintion out of the loop?

(Of course, it is possible to move it out, only that I can't accomplish anything.)

  • 2
    I think you are looking for the `str` function of `istringstream`. See also here: [In C++, how do you clear a stringstream variable?](http://stackoverflow.com/questions/20731/in-c-how-do-you-clear-a-stringstream-variable) – jogojapan Jun 12 '13 at 08:07
  • 3
    You mean http://en.cppreference.com/w/cpp/io/basic_istringstream/str? – R. Martinho Fernandes Jun 12 '13 at 08:07

1 Answers1

2

You cannot call an object constructor after the object declaration. Furthermore std::istringstream::operator ()(std::string) is not (usually) declared anywhere.

Use std::istringstream::str(…) to assign its content after construction.

David Foerster
  • 1,461
  • 1
  • 14
  • 23