Issues with setfill in a member function
I am trying to create a program over multiple files that reads out the time, but I am having trouble displaying the time in the desired format. More specifically, setfill
appears to be causing me problems.
Here is the beginning of a very long error messages I recieve when compiling:
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"))’
Now, this message only appears when I have setfill
in my member function. If I remove setfill
there is no issues with output except the format is wrong.
The member function is:
Void Time::print()
{
cout << setw (2) << setfill ("0") << hours << ":";
cout << setw (2) << setfill ("0") << minutes << ":";
cout << setw (2) << setfill ("0") << seconds << endl;
}
To be clear, I have included iomanip
and setw
has no problem working on its own.
Thanks.
In addition, if you are using a wstringstream, setfill wants a wchar.
Compare
std::stringstream ss;
ss << std::setw(2) << std::setfill('0') << hours << ":";
with
std::wstringstream ss;
ss << std::setw(2) << std::setfill(L'0') << hours << ":";
setfill takes a char, it should be '0'
instead of "0"
You should:
cout << setw (2) << setfill ('0') << hours << ":";
cout << setw (2) << setfill ('0') << minutes << ":";
cout << setw (2) << setfill ('0') << seconds << endl;
setfill takes a char
, not a char*
, so it should be '0'
.
精彩评论