我试图在我的日志记录类中实现我自己的流操纵器。它基本上是改变旗子状态的端线操纵器。然而,当我尝试使用它,我会得到:自定义C++操纵器问题
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;
(日志是登录对象的指针)
任何想法?我怀疑这是某种方式连接到一个事实,即操作者实际是在类中,但是这只是我的胡乱猜测......
干杯,
汤姆
编辑:在这里把这个代替,因为评论限制格式。 我试图实现我的streambuf,它工作得很好,但有一个例外:当我尝试打开filebuf追加失败。输出很好地工作,只是附加不出于某种未知的原因。如果我尝试直接使用ofstream和append,它会起作用。任何想法为什么? - Works:
std::ofstream test;
test.open("somefile", std::ios_base::app);
if (!test) throw LogIoEx("Cannon open file for logging");
test << "test" << std::endl;
正确追加“test”。
不起作用:
std::filebuf *fbuf = new std::filebuf();
if (!fbuf->open("somefile", std::ios_base::app)) throw LogIoEx("Cannon open file for logging");
抛出异常,如果我设置用于openmode来了,然后它工作..
干杯
在这种情况下,因为你的后续问题基本上是完全无关的原来的问题,它会是更好地把它别的地方,让事情变得容易混淆别人谁可能稍后在谷歌搜索或其他东西。更重要的是,它会带来更多的关注你的问题:) – bdonlan 2009-11-23 01:20:52