How to overload the ostream operator << to make it work with log4cxx in C++?

前端 未结 3 1775
甜味超标
甜味超标 2020-12-19 01:50

Say I have a class A and an operator<< declared like so:

// A.h
class A
{
    // A stuff
};
std::ostream& operator<<(std::ostream& os, co         


        
相关标签:
3条回答
  • 2020-12-19 02:48

    Alan's suggestion of putting the user-defined operator in the std namespace works. But I prefer putting the user-defined operator in the log4cxx::helpers namespace, which also works. Specifically,

    namespace log4cxx { namespace helpers {
        ostream& operator<<(ostream& os, const A& a);
    } }
    
    0 讨论(0)
  • 2020-12-19 02:48

    I don't have a compiler available right now but i think the problem is caused by trying to use insert operator on a constant string. "A: " << a

    0 讨论(0)
  • 2020-12-19 02:49

    You could try declaring your operator << in namespace std (that's legal, since you're passing an instance of your user-defined type):

    namespace std {
       ostream& operator<<(ostream& os, const A& a);
    }
    
    0 讨论(0)
提交回复
热议问题