【问题标题】:Passing std::left as an argument传递 std::left 作为参数
【发布时间】:2017-06-27 15:57:38
【问题描述】:

我有一个显示某些内容的函数,其中字符位置可能不同。函数如下所示:

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' 有什么不同?

【问题讨论】:

    标签: c++ io stream output manipulators


    【解决方案1】:

    像大多数stream manipulators 一样,std::left 是一个函数,而不是一个变量。 display 需要改为

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

    为了接受leftrightinternal,或任何其他std::ios_base &(std::ios_base &)类型的操纵器。

    【讨论】:

      猜你喜欢
      • 2014-03-05
      • 1970-01-01
      • 2016-03-31
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2016-03-13
      • 1970-01-01
      • 2018-04-20
      相关资源
      最近更新 更多