1
我通過引用傳遞了一個從cin彙編到類函數的stringstream。當我在類函數中調用streamstream.str()
時,所有的輸入都在那裏。但是,在下一行中,當我撥打getline(stringstream,output)
時,它失敗並且從不啓動循環。我的stringstream有什麼問題?Stringstream允許在函數中調用getline函數
#include <iostream>
#include <sstream>
using namespace std;
class HRMLMap{
public:
HRMLMap(stringstream& iss){
string line;
cout << iss.str() << endl;
while(getline(iss,line)){
cout << "it never gets here." << "\n";
}
}
};
int main() {
std::string line;
stringstream ss("", ios_base::app | ios_base::out);
for(int i = 0; i < 4; ++i){
getline(cin,line);
ss << line << endl;
}
HRMLMap myHRMLMap(ss);
return 0;
}
你怎麼知道它不啓動循環?你的「失敗」指示是什麼? –
請顯示演示問題的代碼。另見[mcve]。看起來像你簡化它太多,所以嚴格來說,它並沒有顯示你描述的行爲 – user463035818
聲明SS爲'stringstream ss;'你沒有選擇'in' - 默認應該足夠好 –