Вит*_*аев 0 c++ io stream manipulators output
我有一个显示某些东西的功能,其中字符定位可以不同。函数看起来像这样:
void display(std::ostream & stream, std::ios_base & positioning);
Run Code Online (Sandbox Code Playgroud)
但是,当我尝试像这样调用此函数时
display_header(std::cout, std::left);
Run Code Online (Sandbox Code Playgroud)
我收到以下错误
error: non-const lvalue reference to type 'std::ios_base' cannot bind to a value of unrelated type 'std::ios_base &(std::ios_base &)'
Run Code Online (Sandbox Code Playgroud)
我不明白为什么std::cout可以通过就好了,但std::left拒绝这样做。又有何lvalue reference to type 'std::ios_base'不同lvalue reference to type 'std::ios_base'?
像大多数流操纵器一样,它std::left是一个函数,而不是一个变量。 display将需要更改为
void display(std::ostream & stream, std::ios_base &(*positioning)(std::ios_base &));
Run Code Online (Sandbox Code Playgroud)
为了接受left, right, internal, 或任何其他类型的操纵器std::ios_base &(std::ios_base &)。