看着这个old post我也想把一个ostream放在一起<< operator()但继续运行以编译问题。我已经尝试了命名空间的建议,包括std ::和log4cxx :: helpers,但我无处可去。
我有一个vector<uint8_t>
我想要转储到LOG4CXX_DEBUG()等等。到目前为止我有这个:
ostream& operator<<(ostream& os, const vector<uint8_t>& bs)
{
ios_base::fmtflags oldFlags = os.flags();
streamsize oldPrec = os.precision();
char oldFill = os.fill();
int s = bs.size();
os << setfill('0') << uppercase << hex;
for (int i = 0; i < s; ++i) {
os << "0x" << setw(2) << (int)bs[i] << ' ';
}
os.flags(oldFlags);
os.precision(oldPrec);
os.fill(oldFill);
return os;
}
GNU g ++编译器吐出如下内容:
Compiling StationControllertest.cpp... In file included from /usr/include/log4cxx/logger.h:32:0,
from ./Configuration.h:15,
from StationController.h:4,
from StationControllertest.cpp:14:
/usr/include/log4cxx/helpers/messagebuffer.h: In function ‘std::basic_ostream<char>& log4cxx::helpers::operator<<(log4cxx::helpers::CharMessageBuffer&, const V&) [with V = std::vector<unsigned char>]’:
StationControllertest.cpp:474:5: instantiated from here
/usr/include/log4cxx/helpers/messagebuffer.h:190:47: error: cannot bind ‘std::basic_ostream<char>’ lvalue to ‘std::basic_ostream<char>&&’
/usr/include/c++/4.6/ostream:581:5: error: initializing argument 1 of ‘std::basic_ostream<_CharT, _Traits>& std::operator<<(std::basic_ostream<_CharT, _Traits>&&, const _Tp&) [with _CharT = char, _Traits = std::char_traits<char>, _Tp = std::vector<unsigned char>]’
makefile:120: recipe for target 'StationControllertest.o' failed
make: *** [StationControllertest.o] Error 1
编辑:这是一个示例程序
#include <iostream>
#include <string>
#include <sstream>
#include <iomanip>
#include <vector>
#include <stdint.h>
#include <log4cxx/logger.h>
#include <log4cxx/propertyconfigurator.h>
using namespace std;
using namespace log4cxx;
using namespace log4cxx::helpers;
static LoggerPtr logger(Logger::getLogger("log4test"));
ostream& operator<<(ostream& os, const vector<uint8_t>& bs)
{
ios_base::fmtflags oldFlags = os.flags();
streamsize oldPrec = os.precision();
char oldFill = os.fill();
int s = bs.size();
os << setfill('0') << uppercase << hex;
for (int i = 0; i < s; ++i) {
os << "0x" << setw(2) << (int)bs[i] << ' ';
}
os.flags(oldFlags);
os.precision(oldPrec);
os.fill(oldFill);
return os;
}
int main(int argc, char* argv[])
{
PropertyConfigurator::configure("Log4cxxConfig.cfg");
vector<uint8_t> v = { 0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, 0x08, 0x09, 0x0a };
cout << v << endl;
LOG4CXX_INFO(logger, v);
}
cout语句编译并运行但不是LOG4CXX_INFO宏。谢谢!