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.)
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.