我有以下代码:
#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/