c++ - 重载 operator<<(ostream&, T) 其中 T 是 "enum class MyEnum"

标签 c++ c++11 operator-overloading ostream

我一直在尝试重载运算符<<,这样我就可以用我的枚举类型来做到这一点:

/* enum class Color { ... } */
Color color = GetColor();
std::wcout << L"The color is " << color << std::endl;

输出:

The color is Green.

这是我在 Visual Studio 2013 中编译的一些测试代码:

#include <iostream>
#include <string>

/* (global scope) */
enum class enum_type_1 { e1_enumerand };

inline std::ostream& operator<<(std::ostream& os, enum_type_1 value)
{
    return os << L"e1_enumerand"; // switch-cases omitted
}

class cls
{
public:
    enum class enum_type_2 { e2_enumerand };

    friend inline std::ostream& operator<<(std::ostream& os, enum_type_2 value)
    {
        return os << L"e2_enumerand"; // switch-cases omitted
    }
};

namespace ns
{
    enum class enum_type_3 { e3_enumerand };

    inline std::ostream& operator<<(std::ostream& os, enum_type_3 value)
    {
        return os << L"e3_enumerand"; // switch-cases omitted
    }
}

int _tmain(int argc, wchar_t* argv[])
{
    enum_type_1      e1 = enum_type_1::e1_enumerand;
    cls::enum_type_2 e2 = cls::enum_type_2::e2_enumerand;
    ns::enum_type_3  e3 = ns::enum_type_3::e3_enumerand;

    std::wcout << L"The value of e1 is " << e1 << std::endl;
    std::wcout << L"The value of e2 is " << e2 << std::endl;
    std::wcout << L"The value of e3 is " << e3 << std::endl;

    std::cin.ignore();

    return 0;
}

如果我用“类”替换定义中的“枚举类”,这将编译,但输出是枚举数的整数值,所以我的重载没有被发现。

如果我在(我更喜欢类型安全的枚举)中留下“枚举类”,这将无法编译并出现以下错误:

d:\...\main.cpp(43): error C2679: binary '<<' : no operator found which takes a right-hand operand of type 'enum_type_1' (or there is no acceptable conversion)

(更多错误细节在帖子底部)

我一直在谷歌搜索、阅读和尝试各种排列组合,但我就是无法让它发挥作用。谁能发现我做错了什么?


这是我收到的错误消息的详细信息。请注意,编译器正在通过 ADL 查找我的重载,但不认为它们匹配。签名看起来和我一模一样……WTF?

d:\...\main.cpp(43): error C2679: binary '<<' : no operator found which takes a right-hand operand of type 'enum_type_1' (or there is no acceptable conversion)
          c:\...\ostream(498): could be 'std::basic_ostream<wchar_t,std::char_traits<wchar_t>> &std::basic_ostream<wchar_t,std::char_traits<wchar_t>>::operator <<(std::basic_streambuf<wchar_t,std::char_traits<wchar_t>> *)'
          .
          .
          .
          d:\...\main.cpp(11): or       'std::ostream &operator <<(std::ostream &,enum_type_1)'
          .
          .
          .
          with
          [
              _Ty=enum_type_1
          ]
          while trying to match the argument list '(std::basic_ostream<wchar_t,std::char_traits<wchar_t>>, enum_type_1)'

d:\...\main.cpp(44): error C2679: binary '<<' : no operator found which takes a right-hand operand of type 'cls::enum_type_2' (or there is no acceptable conversion)
          c:\...\ostream(498): could be 'std::basic_ostream<wchar_t,std::char_traits<wchar_t>> &std::basic_ostream<wchar_t,std::char_traits<wchar_t>>::operator <<(std::basic_streambuf<wchar_t,std::char_traits<wchar_t>> *)'
          .
          .
          .
          d:\...\main.cpp(11): or       'std::ostream &operator <<(std::ostream &,enum_type_1)'
          d:\...\main.cpp(21): or       'std::ostream &operator <<(std::ostream &,cls::enum_type_2)' [found using argument-dependent lookup]
          .
          .
          .
          c:\...\ostream(988): or       'std::basic_ostream<wchar_t,std::char_traits<wchar_t>> &std::operator <<<wchar_t,std::char_traits<wchar_t>,cls::enum_type_2>(std::basic_ostream<wchar_t,std::char_traits<wchar_t>> &&,const _Ty &)'
          with
          [
              _Ty=cls::enum_type_2
          ]
          while trying to match the argument list '(std::basic_ostream<wchar_t,std::char_traits<wchar_t>>, cls::enum_type_2)'

d:\...\main.cpp(45): error C2679: binary '<<' : no operator found which takes a right-hand operand of type 'ns::enum_type_3' (or there is no acceptable conversion)
          c:\...\ostream(498): could be 'std::basic_ostream<wchar_t,std::char_traits<wchar_t>> &std::basic_ostream<wchar_t,std::char_traits<wchar_t>>::operator <<(std::basic_streambuf<wchar_t,std::char_traits<wchar_t>> *)'
          .
          .
          .
          d:\...\main.cpp(11): or       'std::ostream &operator <<(std::ostream &,enum_type_1)'
          d:\...\main.cpp(31): or       'std::ostream &ns::operator <<(std::ostream &,ns::enum_type_3)' [found using argument-dependent lookup]
          .
          .
          .
          c:\...\ostream(988): or       'std::basic_ostream<wchar_t,std::char_traits<wchar_t>> &std::operator <<<wchar_t,std::char_traits<wchar_t>,ns::enum_type_3>(std::basic_ostream<wchar_t,std::char_traits<wchar_t>> &&,const _Ty &)'
          with
          [
              _Ty=ns::enum_type_3
          ]
          while trying to match the argument list '(std::basic_ostream<wchar_t,std::char_traits<wchar_t>>, ns::enum_type_3)'

最佳答案

std::ostreamstd::basic_ostream<char> 的别名.

std::wcout类型为 std::basic_ostream<wchar_t> ,也称为 std::wostream .请注意不同的字符类型。

如果你想要你的 operator<<std::wcout一起工作, 它应该返回 std::wostream&而不是 std::ostream& .

关于c++ - 重载 operator<<(ostream&, T) 其中 T 是 "enum class MyEnum",我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/28978768/

相关文章:

c++ - 不能将变量声明为抽象类型

c++ - 哪些有效的 C++ 项目可以通过 C++11 更好地实现?如何?

templates - C++ : Generic factory that can call any constructor?

c++ - 为什么删除 Actor 指针会导致 "Program.exe has triggered a breakpoint"

C++/Qt memcpy 与 QSharedMemory 崩溃

C++:如何组合多个概念?

c++ - 类似于 C++ 中 Perl 的 AUTOLOAD 的功能

c++ - io_service 在销毁时挂起

c++ - 是否可以在头文件中重载运算符(在声明类时)?

C++ 模板元编程 : overloading operators