//############################################################################################################################################### // CLASS EXEMPLE CODES SOURCES //############################################################################################################################################### //classe exemple class cl_ExempleCS { //================================================================================== // PUBLIC //================================================================================== public: class cl_TestForEnum { //================================================================================== // PUBLIC //================================================================================== public: //---------------------------------------- //enum exemple //---------------------------------------- enum en_MonEnumeration { eME_NoDefine = -1, eME_Premier, eME_Second, // ... }; //================================================================================== // PRIVATE //================================================================================== private: enum en_MonEnumeration m_value; //================================================================================== // PUBLIC //================================================================================== public: //---------------------------------------- // Constructeur //---------------------------------------- cl_TestForEnum() : m_value(en_MonEnumeration::eME_NoDefine) { } //---------------------------------------- // OPERATOR = (enum) <- tien on devrait pourvoir faire un template là //---------------------------------------- enum en_MonEnumeration operator = (enum en_MonEnumeration value) { return (this->m_value = value); } //---------------------------------------- // OPERATOR = (int) //---------------------------------------- int operator = (int value) { return (this->m_value = (enum en_MonEnumeration)value); } //---------------------------------------- // OPERATOR == (int) //---------------------------------------- bool operator == (int value) { return (this->m_value == (enum en_MonEnumeration)value); } //---------------------------------------- // OPERATOR << de enum en_MonEnumeration //---------------------------------------- std::ostream& operator << (std::ostream& O) { switch (this->m_value) { case eME_NoDefine: O << "NoDefine"; break; case eME_Premier: O << "Premier"; break; case eME_Second: O << "Second"; break; default: O << "ERREUR (value=" << this->m_value << ") !"; break; } return O; } }; //================================================================================== // PRIVATE //================================================================================== private: cl_TestForEnum m_ValEnum; //... //================================================================================== // PUBLIC //================================================================================== public: //---------------------------------------- // Constructeur //---------------------------------------- cl_ExempleCS() : m_ValEnum() { } // ... //---------------------------------------- // OPERATOR << //---------------------------------------- std::ostream& operator << (std::ostream& O) { O << "ValEnum=" << this->m_ValEnum << ";"; // ... return O; } };
/tmp/Answer.cpp: In member function ‘std::ostream& cl_ExempleCS::operator<<(std::ostream&)’: /tmp/Answer.cpp:150:25: error: no match for ‘operator<<’ (operand types are ‘std::basic_ostream<char>’ and ‘cl_ExempleCS::cl_TestForEnum’) O << "ValEnum=" << this->m_ValEnum << ";"; ~~~~~~~~~~~~~~~~^~~~~~~~~~~~~~~~~~ In file included from /usr/include/c++/7/iostream:39:0, from /tmp/Answer.cpp:4: /usr/include/c++/7/ostream:108:7: note: candidate: std::basic_ostream<_CharT, _Traits>::__ostream_type& std::basic_ostream<_CharT, _Traits>::operator<<(std::basic_ostream<_CharT, _Traits>::__ostream_type& (*)(std::basic_ostream<_CharT, _Traits>::__ostream_type&)) [with _CharT = char; _Traits = std::char_traits<char>; std::basic_ostream<_CharT, _Traits>::__ostream_type = std::basic_ostream<char>] operator<<(__ostream_type& (*__pf)(__ostream_type&)) ^~~~~~~~ /usr/include/c++/7/ostream:108:7: note: no known conversion for argument 1 from ‘cl_ExempleCS::cl_TestForEnum’ to ‘std::basic_ostream<char>::__ostream_type& (*)(std::basic_ostream<char>::__ostream_type&) {aka std::basic_ostream<char>& (*)(std::basic_ostream<char>&)}’ ... annot convert ‘((cl_ExempleCS*)this)->cl_ExempleCS::m_ValEnum’ (type ‘cl_ExempleCS::cl_TestForEnum’) to type ‘const char*’ O << "ValEnum=" << this->m_ValEnum << ";"; ~~~~~~^~~~~~~~~ In file included from /usr/include/c++/7/iostream:39:0, from /tmp/Answer.cpp:4: /usr/include/c++/7/ostream:569:5: note: candidate: template<class _Traits> std::basic_ostream<char, _Traits>& std::operator<<(std::basic_ostream<char, _Traits>&, const signed char*) operator<<(basic_ostream<char, _Traits>& __out, const signed char* __s) ^~~~~~~~ /usr/include/c++/7/ostream:569:5: note: template argument deduction/substitution failed: /tmp/Answer.cpp:150:34: note: cannot convert ‘((cl_ExempleCS*)this)->cl_ExempleCS::m_ValEnum’ (type ‘cl_ExempleCS::cl_TestForEnum’) to type ‘const signed char*’ O << "ValEnum=" << this->m_ValEnum << ";"; ~~~~~~^~~~~~~~~ In file included from /usr/include/c++/7/iostream:39:0, from /tmp/Answer.cpp:4: /usr/include/c++/7/ostream:574:5: note: candidate: template<class _Traits> std::basic_ostream<char, _Traits>& std::operator<<(std::basic_ostream<char, _Traits>&, const unsigned char*) operator<<(basic_ostream<char, _Traits>& __out, const unsigned char* __s) ^~~~~~~~ /usr/include/c++/7/ostream:574:5: note: template argument deduction/substitution failed: /tmp/Answer.cpp:150:34: note: cannot convert ‘((cl_ExempleCS*)this)->cl_ExempleCS::m_ValEnum’ (type ‘cl_ExempleCS::cl_TestForEnum’) to type ‘const unsigned char*’ O << "ValEnum=" << this->m_ValEnum << ";"; ~~~~~~^~~~~~~~~ In file included from /usr/include/c++/7/iostream:39:0, from /tmp/Answer.cpp:4: /usr/include/c++/7/ostream:682:5: note: candidate: template<class _Ostream, class _Tp> typename std::enable_if<std::__and_<std::__not_<std::is_lvalue_reference<_Tp> >, std::__is_convertible_to_basic_ostream<_Ostream>, std::__is_insertable<typename std::__is_convertible_to_basic_ostream<_Tp>::__ostream_type, const _Tp&, void> >::value, typename std::__is_convertible_to_basic_ostream<_Tp>::__ostream_type>::type std::operator<<(_Ostream&&, const _Tp&) operator<<(_Ostream&& __os, const _Tp& __x) ^~~~~~~~ /usr/include/c++/7/ostream:682:5: note: template argument deduction/substitution failed: /usr/include/c++/7/ostream: In substitution of ‘template<class _Ostream, class _Tp> typename std::enable_if<std::__and_<std::__not_<std::is_lvalue_reference<_Tp> >, std::__is_convertible_to_basic_ostream<_Ostream>, std::__is_insertable<typename std::__is_convertible_to_basic_ostream<_Tp>::__ostream_type, const _Tp&, void> >::value, typename std::__is_convertible_to_basic_ostream<_Tp>::__ostream_type>::type std::operator<<(_Ostream&&, const _Tp&) [with _Ostream = std::basic_ostream<char>&; _Tp = cl_ExempleCS::cl_TestForEnum]’: /tmp/Answer.cpp:150:34: required from here /usr/include/c++/7/ostream:682:5: error: no type named ‘type’ in ‘struct std::enable_if<false, std::basic_ostream<char>&>’
#include <iostream> class cl_ExempleCS { public: class cl_TestForEnum { public: enum en_MonEnumeration { eME_NoDefine = -1, eME_Premier, eME_Second, }; cl_TestForEnum() : m_value(en_MonEnumeration::eME_NoDefine){} void print(std::ostream& out) const { switch (m_value) { case eME_NoDefine: out << "NoDefine"; break; case eME_Premier: out << "Premier"; break; case eME_Second: out << "Second"; break; default: out << "ERREUR (value=" << m_value << ") !"; break; } } friend std::ostream& operator<<(std::ostream& out, const cl_TestForEnum& obj) { obj.print(out); return out; } private: enum en_MonEnumeration m_value; }; public: cl_ExempleCS() : m_ValEnum(){} void print(std::ostream& out) const { out << "ValEnum=" << m_ValEnum << ";"; } friend std::ostream& operator<<(std::ostream& out, const cl_ExempleCS& obj) { obj.print(out); return out; } private: cl_TestForEnum m_ValEnum; }; int main() { cl_ExempleCS test; std::cout << test << std::endl; return 0; }
<<l'ostream est à utiliser à gauche de
<<. On ne peut donc pas définir cet opérateur comme un membre, il faut impérativement définir une fonction externe ayant deux paramètres.
Vous n’avez pas trouvé la réponse que vous recherchez ?
Posez votre question