C++ 如何使ostream操作符过载<&书信电报;使其在C++;?

C++ 如何使ostream操作符过载<&书信电报;使其在C++;?,c++,operator-overloading,log4cxx,C++,Operator Overloading,Log4cxx,假设我有一个类a和一个运算符我现在没有可用的编译器,但我认为问题是由于试图对常量字符串使用insert运算符造成的“A:“您可以尝试声明您的运算符Alan将用户定义的运算符放在std命名空间中的建议有效。但是我更喜欢将用户定义的操作符放在log4cxx::helpers名称空间中,这样也可以工作。具体来说, namespace log4cxx { namespace helpers { ostream& operator<<(ostream& os, cons

假设我有一个类a和一个运算符我现在没有可用的编译器,但我认为问题是由于试图对常量字符串使用insert运算符造成的
“A:“您可以尝试声明您的运算符Alan将用户定义的运算符放在
std
命名空间中的建议有效。但是我更喜欢将用户定义的操作符放在
log4cxx::helpers
名称空间中,这样也可以工作。具体来说,

namespace log4cxx { namespace helpers {
    ostream& operator<<(ostream& os, const A& a);
} }
namespace log4cxx{namespace helpers{

奥斯特雷姆,operator@Alan斯托克斯:我正在使用VisualStudio2010try在messagebuffer之前包含A.h。h@Juraj布拉霍:你认为为什么会这样?在编译器执行查找时,我的运算符是完全定义和已知的。@为什么先生:错误消息表明它是未知/可见的。@Juraj布拉霍:确实如此,但我认为它是相关的事实上,错误指向一个特殊的
运算符,或者我没有插入一个常量字符串;您一定误读了
LOG4CXX_INFO
扩展。我不习惯使用一个不属于我的名称空间,但我应该承认它非常有效!干杯。我很想了解为什么编译器无法在其中找到我的运算符我也是!我认为ADL(参数相关查找)应该让你的代码正常工作;但是ADL充满了微妙之处,Visual Studio也增加了它自己的特性。命名空间std技巧以前对我很有效。一旦你习惯了它,它就不会像看上去那么糟糕了。只有在最近的封闭命名空间中没有匹配项的情况下,才会考虑ADL。在阅读时,我学到了很多这方面的知识G
LoggerPtr logger(LogManager::getLogger("ThisObject"));
A a;
LOG4CXX_INFO(logger, "A: " << a);
// messagebuffer.h
template<class V>
std::basic_ostream<char>& operator<<(CharMessageBuffer& os, const V& val) {
    return ((std::basic_ostream<char>&) os) << val;
}
#define LOG4CXX_INFO(logger, message) { \
    if (logger->isInfoEnabled()) {\
       ::log4cxx::helpers::MessageBuffer oss_; \
       logger->forcedLog(::log4cxx::Level::getInfo(), oss_.str(oss_ << message), LOG4CXX_LOCATION); }}
// messagebuffer.h
template<class V>
std::ostream& operator<<(MessageBuffer& os, const V& val) {
    return ((std::ostream&) os) << val;
}
namespace std {
   ostream& operator<<(ostream& os, const A& a);
}
namespace log4cxx { namespace helpers {
    ostream& operator<<(ostream& os, const A& a);
} }