自定义C ++操纵器问题

时间:2009-11-16 04:13:38

标签: c++ stream manipulators

我正在尝试在我的日志记录类中实现自己的流操纵器。它基本上是改变旗帜状态的终点操纵器。但是,当我尝试使用它时,我会得到:

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并附加它可行。知道为什么吗? - 工作原理:

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然后它可以工作..

干杯

3 个答案:

答案 0 :(得分:5)

定义了operator<<(ostream &, ostream &(*)(ostream&))但未定义operator<<(ostream &, ostream &(Log::*)(ostream&))。也就是说,如果操纵器是普通(非成员)函数,它将起作用,但由于它依赖于Log的实例,因此正常的重载将不起作用。

要解决此问题,您可能需要将log->endl作为辅助对象的实例,并在使用operator<<推送时调用相应的代码。

像这样:

class Log {
  class ManipulationHelper {  // bad name for the class...
  public:
    typedef ostream &(Log::*ManipulatorPointer)(ostream &);

    ManipulationHelper(Log *logger, ManipulatorPointer func) :
      logger(logger),
      func(func) {
    }

    friend ostream &operator<<(ostream &stream, ManipulationHelper helper) {
        // call func on logger
        return (helper.logger)->*(helper.func)(stream);
    }

    Log *logger;
    ManipulatorPointer func;
  }

  friend class ManipulationHelper;

public:
  // ...

  ManipulationHelper endl;

private:
  // ...

  std::ostream& make_endl(std::ostream& out); // renamed
};

// ...

Log::Log(...) {
  // ...
  endl(this, make_endl) {
  // ...
}

答案 1 :(得分:2)

这不是操纵者的工作方式 - 这完全取决于类型。你想要的是:

class Log {
...
struct endl_tag { /* tag struct; no members */ };
static const struct endl_tag endl;
...
LogStream &debug() { /* somehow produce a LogStream type here */ }
}

LogStream &operator<<(LogStream &s, const struct endl_tag &) {
  s.m_newLine = true;
}

请注意:

  1. 由于m_newLine是Log的一部分,因此我们无法使用通用std::ostream。毕竟,std::cout << Log->endl()意味着什么?所以你需要创建一个派生自std::ostream的新流类型(我把它留在这里,但假设它名为LogStream)。
  2. endl实际上并没有做任何事情;所有工作都在operator<<。它的唯一目的是让正确的operator<<重载运行。
  3. 那就是说,你不应该定义新的操纵器和流类,如果你可以避免它,因为它变得复杂:)你能用std::endl做你需要的东西,并包裹{{ 1}}围绕您自己的自定义streambuf?这就是如何使用C ++ IO库。

答案 2 :(得分:1)

试试这个:

#include <iostream>

class Log
{
    public:
    class LogEndl
    {
        /*
         * A class for manipulating a stream that is associated with a log.
         */
        public:
            LogEndl(Log& p)
                :parent(p)
            {}
        private:
            friend std::ostream& operator<<(std::ostream& str,Log::LogEndl const& end);
            Log&    parent;
    };
    std::ostream& debug()   {return std::cout;}
    /*
     * You are not quite using manipulators the way they are entended.
     * But I wanted to give an example that was close to your original
     *
     * So return an object that has an operator << that knows what to do.
     * To feed back info to the Log it need to keep track of who its daddy is.
     */
    LogEndl       endl()    {return LogEndl(*this);}
    private:
        friend std::ostream& operator<<(std::ostream& str,Log::LogEndl const& end);
        bool    endOfLine;

};


std::ostream& operator<<(std::ostream& str,Log::LogEndl const& end)
{
    // Stick stuff on the stream here.
    str << std::endl;

    // Make any notes you need in the log class here.
    end.parent.endOfLine    = true;

    return str;
};

int main()
{
    Log     log;

    /*
     * Please note the use of objects rather than pointers here
     * It may help
     */
    log.debug() << "Debug " << log.endl();
}