我一直在寻找解决方案,但找不到我需要/想要的。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