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

隐身守侯 提交于 2019-11-29 09:42:01

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);
}

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);
} }

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

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!