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

标签 c++ string 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(…) 在构造后分配其内容。

关于C++ - 无法将 istringstream 定义移出循环,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/17060558/

相关文章:

c++ - 插入的最有效数据结构,然后按不同条件排序

c++ - Resharper: "some control paths may leak this resource acquisition"C++

c++ - 链接 Boost 给我错误

java - 在保留文字的同时修剪空格

C++使用istringstream将整数读取为无符号字符

c++ - 如何在数组中初始化此类?

c++ - std::string 内存泄漏

C++字符串混淆

c++ - 如何将 istringstream 和 ifstream 分配给 istream 变量?

c++ - 如何从stringstream中跳过空格和换行符并将它们放入 vector 中?