C++ - 无法将 istringstream 定义移出循环



我有以下一段代码:

#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");
}

生成以下错误:

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

我知道可以通过在循环内使用istringstream iss(inp);来避免这个问题,但是,不可能将这个定义移出循环吗?

(当然,如果可以将其移出,只是我不能完成任何事情。)

不能在对象声明之后调用对象构造函数。此外,std::istringstream::operator ()(std::string)(通常)不在任何地方声明。

施工后用std::istringstream::str(…)分配其内容

最新更新