2011-03-31 45 views
2

我試圖通過多個文件創建一個程序來讀出時間,但我無法以所需的格式顯示時間。更具體地說,setfill似乎導致我的問題。成員函數中setfill的問題

以下是編譯時我收到一個很長的錯誤消息的開頭:

error: no match for ‘operator<<’ in ‘std::operator<< [with _CharT = char, 
_Traits = std::char_traits<char>](((std::basic_ostream<char, 
std::char_traits<char> >&)(& std::cout)), std::setw(2)) << std::setfill 
[with _CharT = const char*](((const char*)"0"))’ 

現在,當我在成員函數有setfill纔會出現此消息。如果我刪除setfill,則輸出沒有問題,除非格式錯誤。

成員函數是:

Void Time::print() 
{ 
    cout << setw (2) << setfill ("0") << hours << ":"; 
    cout << setw (2) << setfill ("0") << minutes << ":"; 
    cout << setw (2) << setfill ("0") << seconds << endl; 
} 

要清楚,我已經包括iomanipsetw沒有問題對自己的工作。

謝謝。

回答

4

setfill需要一個char,它應該是'0'代替"0"

1

setfill需要char,不是char*,所以它應該是'0'

+1

注意,' 「0」'是'爲const char *',而不是一個'的char *'。 – 2011-03-31 08:01:34

2

您應該:

cout << setw (2) << setfill ('0') << hours << ":"; 
cout << setw (2) << setfill ('0') << minutes << ":"; 
cout << setw (2) << setfill ('0') << seconds << endl; 
4

此外,如果您使用的是wstringstream,setfill想要一個WCHAR。

比較

std::stringstream ss; 
ss << std::setw(2) << std::setfill('0') << hours << ":"; 

std::wstringstream ss; 
ss << std::setw(2) << std::setfill(L'0') << hours << ":";