c++ - Log4cxx << 运算符导致访问冲突
问题描述
我在 VS2008 中有一个 dll 项目,这会导致我的应用程序引发访问冲突。我调试并发现在 Log4cxx 行中,LOG4CXX_DEBUG(logger, myMessage << doubleA << double B);
运算符 << 导致了问题。所有记录器级别都是这种情况。我想通过覆盖 << 运算符来完成这项工作。但这似乎比说的要难。我在 StackOverflow 中查找了这个以及我到目前为止所尝试的内容:
我尝试在Log4cxx
命名空间中覆盖它:
namespace log4cxx { namespace helpers {
ostream& operator<<(ostream& os, const double& a);
} }
这不编译:
more than one operator "<<" matches these operands:
function "log4cxx::helpers::operator<<(std::ostream &os, const double &a)"
function "std::basic_ostream<_Elem, _Traits>::operator<<(double _Val) [with _Elem=char, _Traits=std::char_traits<char>]"
operand types are: std::basic_ostream<char, std::char_traits<char>> << const double
我尝试在 Std 命名空间中覆盖它(我知道不推荐这样做):
namespace std {
ostream& operator<<(ostream& os, const double& a);
}
这抛出:
more than one operator "<<" matches these operands:
function "std::operator<<(std::ostream &os, const double &a)"
function "std::basic_ostream<_Elem, _Traits>::operator<<(double _Val) [with _Elem=char, _Traits=std::char_traits<char>]"
operand types are: std::basic_ostream<char, std::char_traits<char>> << const double
知道如何正确重载运算符并使其工作吗?
我尝试的另一件事是:我尝试在消息字符串进入记录器行之前构建它。
string myMessage = "myMessage" + double A + doubleB;
然后使用这个字符串作为LOG4CXX_DEBUG(logger, myMessage);
This 什么都不做。有同样的访问冲突。而我尝试...
std::ostringstream os;
os << "myMessage here << doubleX<< ", valuey=" << doubleY;
LOG4CXX_DEBUG(logger, os.str());
这里也有同样的访问冲突。我也设法log4cxx
用 VS2008 构建了,我认为这可能是问题,因为我一直在使用用 VS2005 构建的那个。
你能在这里帮忙吗?我还能做些什么来完成这项工作?
解决方案
std 已经有 double 的重载,你不能再引入另一个。你可以做的是在一个类中重载它。