【发布时间】:2009-11-16 04:13:38
【问题描述】:
我正在尝试在我的日志记录类中实现我自己的流操纵器。它基本上是改变标志状态的端线操纵器。但是,当我尝试使用它时,我会得到:
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 则它可以工作..
干杯
【问题讨论】:
-
在这种情况下,由于您的后续问题基本上与原始问题完全无关,因此最好将其放在其他地方,以免其他人可能会在稍后出现谷歌搜索什么的。更重要的是,它会让你的问题得到更多关注:)
标签: c++ stream manipulators