ERROR! /tmp/h7o8IuG5pb/main.cpp: In function 'int main()': /tmp/h7o8IuG5pb/main.cpp:25:10: error: no match for 'operator<<' (operand types are 'std::ostream' {aka 'std::basic_ostream<char>'} and 'Day') 25 | cout << today; | ~~~~ ^~ ~~~~~ | | | | | Day | std::ostream {aka std::basic_ostream<char>} In file included from /usr/local/include/c++/14.2.0/iostream:41, from /tmp/h7o8IuG5pb/main.cpp:1: /usr/local/include/c++/14.2.0/ostream:116:7: note: candidate: 'std::basic_ostream<_CharT, _Traits>::__ostream_type& std::basic_ostream<_CharT, _Traits>::operator<<(__ostream_type& (*)(__ostream_type&)) [with _CharT = char; _Traits = std::char_traits<char>; __ostream_type = std::basic_ostream<char>]' 116 | operator<<(__ostream_type& (*__pf)(__ostream_type&)) | ^~~~~~~~ . . . .
然後初始化時也不小心寫成傳統形式 Day today = Thursday;,沒有考慮到作用域規則的話,也會產生錯誤:
1 2 3 4 5 6 7 8 9
ERROR! /tmp/286k2LGl0z/main.cpp: In function 'int main()': /tmp/286k2LGl0z/main.cpp:21:17: error: 'Thursday' was not declared in this scope; did you mean 'Day::Thursday'? 21 | Day today = Thursday; | ^~~~~~~~ | Day::Thursday /tmp/286k2LGl0z/main.cpp:12:5: note: 'Day::Thursday' declared here 12 | Thursday, | ^~~~~~~~
ostream& operator<<(std::ostream& os, Color c) { switch (c) { case Color::RED: os << "RED"; break; case Color::GREEN: os << "GREEN"; break; case Color::BLUE: os << "BLUE"; break; default: os << "UNKNOWN"; break; } return os; }