我正在尝试在我的日志记录类中实现我自己的流操纵器。它基本上是改变标志状态的端线操纵器。但是,当我尝试使用它时,我会得到:
ftypes.cpp:57: error: no match for ‘operator<<’ in ‘log->Log::debug() << log->Log::endl’
/usr/lib/gcc/i386-redhat-linux/4.1.2/../../../../include/c++/4.1.2/bits/ostream.tcc:67: note: candidates are: std::basic_ostream<_CharT, _Traits>& std::basic_ostream<_CharT, _Traits>::operator<<(std::basic_ostream<_CharT, _Traits>& (*)(std::basic_ostream<_CharT, _Traits>&)) [with _CharT = char, _Traits = std::char_traits<char>]
/usr/lib/gcc/i386-redhat-linux/4.1.2/../../../../include/c++/4.1.2/bits/ostream.tcc:78: note: std::basic_ostream<_CharT, _Traits>& std::basic_ostream<_CharT, _Traits>::operator<<(std::basic_ios<_CharT, _Traits>& (*)(std::basic_ios<_CharT, _Traits>&)) [with _CharT = char, _Traits = std::char_traits<char>]
/usr/lib/gcc/i386-redhat-linux/4.1.2/../../../../include/c++/4.1.2/bits/ostream.tcc:90: note: std::basic_ostream<_CharT, _Traits>& std::basic_ostream<_CharT, _Traits>::operator<<(std::ios_base& (*)(std::ios_base&)) [with _CharT = char, _Traits = std::char_traits<char>]
...
代码:
class Log {
public:
...
std::ostream& debug() { return log(logDEBUG); }
std::ostream& endl(std::ostream& out); // manipulator
...
private:
...
std::ofstream m_logstream;
bool m_newLine;
...
}
std::ostream& Log::endl(std::ostream& out)
{
out << std::endl;
m_newLine = true;
return out;
}
std::ostream& Log::log(const TLogLevel level)
{
if (level > m_logLevel) return m_nullstream;
if (m_newLine)
{
m_logstream << timestamp() << "|" << logLevelString(level) << "|";
m_newLine = false;
}
return m_logstream;
}
当我尝试调用它时出现错误:
log->debug() << "START - object created" << log->endl;
(log 是指向 Log 对象的指针)
有任何想法吗?我怀疑这与机械手实际上在课堂上的事实有某种联系,但这只是我的疯狂猜测......
干杯,
汤姆
编辑:由于限制格式,将其放在这里而不是评论。我尝试实现我的 streambuf,它运行良好,但有一个例外:当我尝试打开 filebuf 进行追加时,它失败了。输出效果很好,只是由于某些未知原因而没有附加。如果我尝试直接使用 ofstream 和 append 它可以工作。知道为什么吗?– 作品:
std::ofstream test;
test.open("somefile", std::ios_base::app);
if (!test) throw LogIoEx("Cannon open file for logging");
test << "test" << std::endl;
正确附加“测试”。
不起作用:
std::filebuf *fbuf = new std::filebuf();
if (!fbuf->open("somefile", std::ios_base::app)) throw LogIoEx("Cannon open file for logging");
抛出异常,如果我将 openmode 设置为 out 那么它可以工作..
干杯