2017-06-27 87 views
0

我有一個顯示內容的函數,其中的字符位置可以不同。功能如下:將std :: left作爲參數傳遞

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

回答

3

最喜歡stream manipulatorsstd::left是一個函數,而不是一個變量。 display將需要改變

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

爲了接受leftrightinternal或類型std::ios_base &(std::ios_base &)的任何其他機械手。