我試圖在我的日誌記錄類中實現我自己的流操縱器。它基本上是改變旗子狀態的端線操縱器。然而,當我嘗試使用它,我會得到:自定義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