2013-06-12 63 views
0

的我有以下的代碼: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);,然而,是否無法將此定義移出循環?

(當然,可以將其搬走,只是我不能完成任何事情。)

+2

我想你正在尋找'istringstream'的'str'函數。另請參見:[在C++中,如何清除stringstream變量?](http://stackoverflow.com/questions/20731/in-c-how-do-you-clear-a-stringstream-variable) – jogojapan

+3

您意思是http://en.cppreference.com/w/cpp/io/basic_istringstream/str? –

+0

使用'iss.str(s);' – 0x499602D2

回答

3

你不能調用對象的聲明之後的對象構造函數。此外std::istringstream::operator()(std::string)不是(通常)在任何地方聲明。

使用std::istringstream::str(…)在施工後分配其內容。