2015-03-11 2 views
0

J'ai essayé de surcharger l'opérateur < < pour que je puisse le faire avec mes types enum:opérateur Surcharge << (ostream & T) où T est "ENUM classe MyEnum"

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

SORTIE:

The color is Green. 

est ici un code de test je compiler dans 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; 
} 

Si je remplace " enum class "dans les définitions avec" class ", cela va compiler, mais la sortie est les valeurs entières des enumerands, donc ma surcharge n'est pas trouvée.

Si je quitte « classe ENUM » (je préfère les énumérations de type sécurisé), ce ne peut pas compiler tout avec l'erreur suivante:

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) 

(détail d'erreur en bas de post)

J'ai été googler, lire, et essayer toutes sortes de permutations, et je ne peux tout simplement pas obtenir ce travail. Quelqu'un peut-il repérer ce que je fais mal?


Voici le détail des messages d'erreur que je reçois. Notez que le compilateur trouve mes surcharges via ADL, mais ne les considère pas comme une correspondance. Les signatures me semblent identiques ... 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)' 

Répondre

4

std::ostream est un alias pour std::basic_ostream<char>.

std::wcout est de type std::basic_ostream<wchar_t>, également connu sous le nom std::wostream. Notez les différents types de caractères.

Si vous voulez que votre operator<< de travailler avec std::wcout, il faut prendre et retourner std::wostream& plutôt que std::ostream&.

+0

Aargh. J'ai regardé ostream et peux jurer je l'ai vu était std :: basic_ostream