我一直在尋找解決方案,但找不到我需要/想要的。C++簡單流操作與ostream和istream?
我想要做的就是將用於std :: cout的流傳遞給一個處理它的函數。什麼到目前爲止我用的是一個模板函數:
template<typename T>
void printUpdate(T a){
std::cout << "blabla" << a << std::flush;
}
int main(int argc, char** argv){
std::stringstream str;
str << " hello " << 1 + 4 << " goodbye";
printUpdate<>(str.str());
return 0;
}
我寧願是這樣的:
printUpdate << " hello " << 1 + 4 << " goodbye";
或
std::cout << printUpdate << " hello " << 1 + 4 << " goodbye";
我試圖做的事:
void printUpdate(std::istream& a){
std::cout << "blabla" << a << std::flush;
}
但這給了我:
error: invalid operands of types ‘void(std::istream&) {aka void(std::basic_istream<char>&)}’ and ‘const char [5]’ to binary ‘operator<<’
你不能將數據輸出到的輸入流。將參數更改爲'std :: ostream&a'。另外,'flush'沒有爲輸入流定義。 – 2014-10-08 15:25:02
我也試過了。同樣的錯誤: 錯誤:類型'無效(std :: ostream&){aka void(std :: basic_ostream&)}'和'const char [5]'爲二元操作符<<' –
2014-10-08 15:26:41
的操作數無效'main'函數,您需要將流類型提供給'printUpdate'函數調用,例如'printUpdate'。 –
2014-10-08 15:29:50