c++ - 将 std::left 作为参数传递

标签 c++ io stream output manipulators

我有一个显示某些东西的功能,其中字符定位可以不同。函数如下所示:

void display(std::ostream & stream, std::ios_base & positioning);

但是,当我尝试这样调用这个函数时

display_header(std::cout, std::left);

出现以下错误

error: non-const lvalue reference to type 'std::ios_base' cannot bind to a value of unrelated type 'std::ios_base &(std::ios_base &)'

我不明白为什么 std::cout 可以顺利通过,但是 std::left 拒绝这样做。另外,lvalue reference to type 'std::ios_base'lvalue reference to type 'std::ios_base' 有何不同?

最佳答案

最喜欢 stream manipulators , std::left 是函数,不是变量。 display 需要更改为

void display(std::ostream & stream, std::ios_base &(*positioning)(std::ios_base &));

为了接受 leftrightinternalstd::ios_base &( std::ios_base &).

关于c++ - 将 std::left 作为参数传递,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/44784872/

相关文章:

c# - WCF 无法将文件作为流返回

c++ - 运算符重载 "+"和 "="问题 -- school asst

java - Android 工作室抛出 IOException : Operation not permitted

Python-需要快速算法来删除文件中所有派生词,换句话说

c# - 一次反序列化json数组流一项

c# - 为什么我的网络流下载 0 字节?

c++ - 在主代码中调用 .hpp 函数时出错

c++ - 对于这种情况,类级别变量是个好主意吗?

c++ - 如何在命令行上使用 Qt 进行是/否对话?

c# - 使用文本文件保存和加载 TextBox 或其他控件值