c++ - MSVC++ 扩展 iostream 时出错

标签 c++ logging iostream outputstream extending-classes

我正在为我的应用程序编写一个输出管理器类,虽然我的类在 linux 下的 g++ 上运行,但它不会在 MSVC++ 2010 下编译。

这是我的程序的 SSCCE 版本:

#include <iostream>
#include <malloc.h>
#include <sstream>
#include <fstream>

template<class cT, class traits=std::char_traits<cT> >
class BasicE3OutStream: public std::basic_ostream<cT, traits> {
    public:
        BasicE3OutStream() :
                std::basic_ios<cT, traits>(), std::basic_ostream<cT, traits>(0), mBuffer() {
            resetModifiers();
            mLineBuffer=(std::stringstream*) malloc(sizeof(std::stringstream));
            mLineBuffer=new (mLineBuffer) std::stringstream();
        }

        BasicE3OutStream(const BasicE3OutStream<cT,traits> &) {
        }

        virtual ~BasicE3OutStream() {
            delete mLineBuffer;
            mLineBuffer=NULL;
        }

        void resetModifiers() {
            mDebug=false;
            mMemory=false;
        }

        void open(const char* fileName) {
            mBuffer.open(fileName, std::stringstream::out);
        }

        void close() {
            mBuffer.close();
        }

        template<class T>
        BasicE3OutStream& operator<<(T val) {
            (*mLineBuffer)<<val;
            return (*this);
        }

        BasicE3OutStream& operator<<(char* val) {
            (*mLineBuffer)<<val;
            return (*this);
        }

        BasicE3OutStream& operator<<(std::string val) {
            (*mLineBuffer)<<val;
            return (*this);
        }

        void addLineFeed() {
            std::string modifier;
            modifier="";
            if (mDebug)
                modifier+="[DEBUG] ";
            else if (mMemory)
                modifier+="[MEMORY] ";

            mBuffer<<modifier<<mLineBuffer->str()<<"\n";
            mLineBuffer->~basic_stringstream();
            mLineBuffer=new (mLineBuffer) std::stringstream();
            resetModifiers();
        }

        void flush() {
            mBuffer.flush();
            resetModifiers();
        }

        void setFlag(int f) {
            if (f==0)
                mDebug=true;
            else if (f==1)
                mMemory=true;
        }
    public:
        std::basic_ofstream<cT, traits> mBuffer;
        std::stringstream* mLineBuffer;
        bool mDebug, mMemory;
};

template<class cT, class traits, class T>
BasicE3OutStream<cT, traits>& operator<<(BasicE3OutStream<char, traits>& str , T val) {
    str.operator <<(val);
    return (str);
}

typedef BasicE3OutStream<char> E3OutStream;

template<class charT, class traits>
BasicE3OutStream<charT, traits>& endl(BasicE3OutStream<charT, traits>& os) {
    os.addLineFeed();
    return (os);
}

template<class charT, class traits>
BasicE3OutStream<charT, traits>& flush(BasicE3OutStream<charT, traits>& os) {
    os.flush();
    return (os);
}

template<class charT, class traits>
BasicE3OutStream<charT, traits>& debug(BasicE3OutStream<charT, traits>& os) {
    os.setFlag(0);
    return (os);
}

template<class charT, class traits>
BasicE3OutStream<charT, traits>& memory(BasicE3OutStream<charT, traits>& os) {
    os.setFlag(1);
    return (os);
}

/**
 * Io manipulator, allows to use endl and other modificators
 */
template<class charT, class traits> BasicE3OutStream<charT, traits>& operator<<(
        BasicE3OutStream<charT, traits> &s
        , BasicE3OutStream<charT, traits>& (*iomanip)(BasicE3OutStream<charT, traits>&)) {
    return (iomanip(s));
}


int main() {
    E3OutStream s;
    s.open("output.txt");
    s<<debug<<"Debug info"<<endl;
    s<<flush;
    s<<memory<<"Memory info"<<endl;
    s<<flush;
    s.close();
}

它在 g++ 中运行良好,但是在 MSVC 上我得到一个编译器错误:

1>------ Build started: Project: sscce, Configuration: Debug Win32 ------
1>  main.cpp
1>c:\users\guillaume\documents\visual studio 2010\projects\sscce\sscce\main.cpp(129): error C2679: binary '<<' : no operator found which takes a right-hand operand of type 'overloaded-function' (or there is no acceptable conversion)
1>          c:\program files (x86)\microsoft visual studio 10.0\vc\include\ostream(679): could be 'std::basic_ostream<_Elem,_Traits> &std::operator <<<char,std::char_traits<char>>(std::basic_ostream<_Elem,_Traits> &,const char *)' [found using argument-dependent lookup]
1>          with
1>          [
1>              _Elem=char,
1>              _Traits=std::char_traits<char>
1>          ]
1>          c:\program files (x86)\microsoft visual studio 10.0\vc\include\ostream(726): or       'std::basic_ostream<_Elem,_Traits> &std::operator <<<char,std::char_traits<char>>(std::basic_ostream<_Elem,_Traits> &,char)' [found using argument-dependent lookup]
1>          with
1>          [
1>              _Elem=char,
1>              _Traits=std::char_traits<char>
1>          ]
1>          c:\program files (x86)\microsoft visual studio 10.0\vc\include\ostream(764): or       'std::basic_ostream<_Elem,_Traits> &std::operator <<<std::char_traits<char>>(std::basic_ostream<_Elem,_Traits> &,const char *)' [found using argument-dependent lookup]
1>          with
1>          [
1>              _Elem=char,
1>              _Traits=std::char_traits<char>
1>          ]
1>          c:\program files (x86)\microsoft visual studio 10.0\vc\include\ostream(811): or       'std::basic_ostream<_Elem,_Traits> &std::operator <<<std::char_traits<char>>(std::basic_ostream<_Elem,_Traits> &,char)' [found using argument-dependent lookup]
1>          with
1>          [
1>              _Elem=char,
1>              _Traits=std::char_traits<char>
1>          ]
1>          c:\program files (x86)\microsoft visual studio 10.0\vc\include\ostream(937): or       'std::basic_ostream<_Elem,_Traits> &std::operator <<<std::char_traits<char>>(std::basic_ostream<_Elem,_Traits> &,const signed char *)' [found using argument-dependent lookup]
1>          with
1>          [
1>              _Elem=char,
1>              _Traits=std::char_traits<char>
1>          ]
1>          c:\program files (x86)\microsoft visual studio 10.0\vc\include\ostream(944): or       'std::basic_ostream<_Elem,_Traits> &std::operator <<<std::char_traits<char>>(std::basic_ostream<_Elem,_Traits> &,signed char)' [found using argument-dependent lookup]
1>          with
1>          [
1>              _Elem=char,
1>              _Traits=std::char_traits<char>
1>          ]
1>          c:\program files (x86)\microsoft visual studio 10.0\vc\include\ostream(951): or       'std::basic_ostream<_Elem,_Traits> &std::operator <<<std::char_traits<char>>(std::basic_ostream<_Elem,_Traits> &,const unsigned char *)' [found using argument-dependent lookup]
1>          with
1>          [
1>              _Elem=char,
1>              _Traits=std::char_traits<char>
1>          ]
1>          c:\program files (x86)\microsoft visual studio 10.0\vc\include\ostream(958): or       'std::basic_ostream<_Elem,_Traits> &std::operator <<<std::char_traits<char>>(std::basic_ostream<_Elem,_Traits> &,unsigned char)' [found using argument-dependent lookup]
1>          with
1>          [
1>              _Elem=char,
1>              _Traits=std::char_traits<char>
1>          ]
1>          c:\program files (x86)\microsoft visual studio 10.0\vc\include\ostream(1085): or       'std::basic_ostream<_Elem,_Traits> &std::operator <<<char,std::char_traits<char>>(std::basic_ostream<_Elem,_Traits> &,const std::error_code &)' [found using argument-dependent lookup]
1>          with
1>          [
1>              _Elem=char,
1>              _Traits=std::char_traits<char>
1>          ]
1>          c:\users\guillaume\documents\visual studio 2010\projects\sscce\sscce\main.cpp(43): or       'BasicE3OutStream<cT> &BasicE3OutStream<cT>::operator <<(char *)'
1>          with
1>          [
1>              cT=char
1>          ]
1>          c:\users\guillaume\documents\visual studio 2010\projects\sscce\sscce\main.cpp(48): or       'BasicE3OutStream<cT> &BasicE3OutStream<cT>::operator <<(std::string)'
1>          with
1>          [
1>              cT=char
1>          ]
1>          while trying to match the argument list '(E3OutStream, overloaded-function)'
1>c:\users\guillaume\documents\visual studio 2010\projects\sscce\sscce\main.cpp(130): error C2679: binary '<<' : no operator found which takes a right-hand operand of type 'overloaded-function' (or there is no acceptable conversion)
1>          c:\program files (x86)\microsoft visual studio 10.0\vc\include\ostream(679): could be 'std::basic_ostream<_Elem,_Traits> &std::operator <<<char,std::char_traits<char>>(std::basic_ostream<_Elem,_Traits> &,const char *)' [found using argument-dependent lookup]
1>          with
1>          [
1>              _Elem=char,
1>              _Traits=std::char_traits<char>
1>          ]
1>          c:\program files (x86)\microsoft visual studio 10.0\vc\include\ostream(726): or       'std::basic_ostream<_Elem,_Traits> &std::operator <<<char,std::char_traits<char>>(std::basic_ostream<_Elem,_Traits> &,char)' [found using argument-dependent lookup]
1>          with
1>          [
1>              _Elem=char,
1>              _Traits=std::char_traits<char>
1>          ]
1>          c:\program files (x86)\microsoft visual studio 10.0\vc\include\ostream(764): or       'std::basic_ostream<_Elem,_Traits> &std::operator <<<std::char_traits<char>>(std::basic_ostream<_Elem,_Traits> &,const char *)' [found using argument-dependent lookup]
1>          with
1>          [
1>              _Elem=char,
1>              _Traits=std::char_traits<char>
1>          ]
1>          c:\program files (x86)\microsoft visual studio 10.0\vc\include\ostream(811): or       'std::basic_ostream<_Elem,_Traits> &std::operator <<<std::char_traits<char>>(std::basic_ostream<_Elem,_Traits> &,char)' [found using argument-dependent lookup]
1>          with
1>          [
1>              _Elem=char,
1>              _Traits=std::char_traits<char>
1>          ]
1>          c:\program files (x86)\microsoft visual studio 10.0\vc\include\ostream(937): or       'std::basic_ostream<_Elem,_Traits> &std::operator <<<std::char_traits<char>>(std::basic_ostream<_Elem,_Traits> &,const signed char *)' [found using argument-dependent lookup]
1>          with
1>          [
1>              _Elem=char,
1>              _Traits=std::char_traits<char>
1>          ]
1>          c:\program files (x86)\microsoft visual studio 10.0\vc\include\ostream(944): or       'std::basic_ostream<_Elem,_Traits> &std::operator <<<std::char_traits<char>>(std::basic_ostream<_Elem,_Traits> &,signed char)' [found using argument-dependent lookup]
1>          with
1>          [
1>              _Elem=char,
1>              _Traits=std::char_traits<char>
1>          ]
1>          c:\program files (x86)\microsoft visual studio 10.0\vc\include\ostream(951): or       'std::basic_ostream<_Elem,_Traits> &std::operator <<<std::char_traits<char>>(std::basic_ostream<_Elem,_Traits> &,const unsigned char *)' [found using argument-dependent lookup]
1>          with
1>          [
1>              _Elem=char,
1>              _Traits=std::char_traits<char>
1>          ]
1>          c:\program files (x86)\microsoft visual studio 10.0\vc\include\ostream(958): or       'std::basic_ostream<_Elem,_Traits> &std::operator <<<std::char_traits<char>>(std::basic_ostream<_Elem,_Traits> &,unsigned char)' [found using argument-dependent lookup]
1>          with
1>          [
1>              _Elem=char,
1>              _Traits=std::char_traits<char>
1>          ]
1>          c:\program files (x86)\microsoft visual studio 10.0\vc\include\ostream(1085): or       'std::basic_ostream<_Elem,_Traits> &std::operator <<<char,std::char_traits<char>>(std::basic_ostream<_Elem,_Traits> &,const std::error_code &)' [found using argument-dependent lookup]
1>          with
1>          [
1>              _Elem=char,
1>              _Traits=std::char_traits<char>
1>          ]
1>          c:\users\guillaume\documents\visual studio 2010\projects\sscce\sscce\main.cpp(43): or       'BasicE3OutStream<cT> &BasicE3OutStream<cT>::operator <<(char *)'
1>          with
1>          [
1>              cT=char
1>          ]
1>          c:\users\guillaume\documents\visual studio 2010\projects\sscce\sscce\main.cpp(48): or       'BasicE3OutStream<cT> &BasicE3OutStream<cT>::operator <<(std::string)'
1>          with
1>          [
1>              cT=char
1>          ]
1>          while trying to match the argument list '(E3OutStream, overloaded-function)'
1>c:\users\guillaume\documents\visual studio 2010\projects\sscce\sscce\main.cpp(131): error C2679: binary '<<' : no operator found which takes a right-hand operand of type 'overloaded-function' (or there is no acceptable conversion)
1>          c:\program files (x86)\microsoft visual studio 10.0\vc\include\ostream(679): could be 'std::basic_ostream<_Elem,_Traits> &std::operator <<<char,std::char_traits<char>>(std::basic_ostream<_Elem,_Traits> &,const char *)' [found using argument-dependent lookup]
1>          with
1>          [
1>              _Elem=char,
1>              _Traits=std::char_traits<char>
1>          ]
1>          c:\program files (x86)\microsoft visual studio 10.0\vc\include\ostream(726): or       'std::basic_ostream<_Elem,_Traits> &std::operator <<<char,std::char_traits<char>>(std::basic_ostream<_Elem,_Traits> &,char)' [found using argument-dependent lookup]
1>          with
1>          [
1>              _Elem=char,
1>              _Traits=std::char_traits<char>
1>          ]
1>          c:\program files (x86)\microsoft visual studio 10.0\vc\include\ostream(764): or       'std::basic_ostream<_Elem,_Traits> &std::operator <<<std::char_traits<char>>(std::basic_ostream<_Elem,_Traits> &,const char *)' [found using argument-dependent lookup]
1>          with
1>          [
1>              _Elem=char,
1>              _Traits=std::char_traits<char>
1>          ]
1>          c:\program files (x86)\microsoft visual studio 10.0\vc\include\ostream(811): or       'std::basic_ostream<_Elem,_Traits> &std::operator <<<std::char_traits<char>>(std::basic_ostream<_Elem,_Traits> &,char)' [found using argument-dependent lookup]
1>          with
1>          [
1>              _Elem=char,
1>              _Traits=std::char_traits<char>
1>          ]
1>          c:\program files (x86)\microsoft visual studio 10.0\vc\include\ostream(937): or       'std::basic_ostream<_Elem,_Traits> &std::operator <<<std::char_traits<char>>(std::basic_ostream<_Elem,_Traits> &,const signed char *)' [found using argument-dependent lookup]
1>          with
1>          [
1>              _Elem=char,
1>              _Traits=std::char_traits<char>
1>          ]
1>          c:\program files (x86)\microsoft visual studio 10.0\vc\include\ostream(944): or       'std::basic_ostream<_Elem,_Traits> &std::operator <<<std::char_traits<char>>(std::basic_ostream<_Elem,_Traits> &,signed char)' [found using argument-dependent lookup]
1>          with
1>          [
1>              _Elem=char,
1>              _Traits=std::char_traits<char>
1>          ]
1>          c:\program files (x86)\microsoft visual studio 10.0\vc\include\ostream(951): or       'std::basic_ostream<_Elem,_Traits> &std::operator <<<std::char_traits<char>>(std::basic_ostream<_Elem,_Traits> &,const unsigned char *)' [found using argument-dependent lookup]
1>          with
1>          [
1>              _Elem=char,
1>              _Traits=std::char_traits<char>
1>          ]
1>          c:\program files (x86)\microsoft visual studio 10.0\vc\include\ostream(958): or       'std::basic_ostream<_Elem,_Traits> &std::operator <<<std::char_traits<char>>(std::basic_ostream<_Elem,_Traits> &,unsigned char)' [found using argument-dependent lookup]
1>          with
1>          [
1>              _Elem=char,
1>              _Traits=std::char_traits<char>
1>          ]
1>          c:\program files (x86)\microsoft visual studio 10.0\vc\include\ostream(1085): or       'std::basic_ostream<_Elem,_Traits> &std::operator <<<char,std::char_traits<char>>(std::basic_ostream<_Elem,_Traits> &,const std::error_code &)' [found using argument-dependent lookup]
1>          with
1>          [
1>              _Elem=char,
1>              _Traits=std::char_traits<char>
1>          ]
1>          c:\users\guillaume\documents\visual studio 2010\projects\sscce\sscce\main.cpp(43): or       'BasicE3OutStream<cT> &BasicE3OutStream<cT>::operator <<(char *)'
1>          with
1>          [
1>              cT=char
1>          ]
1>          c:\users\guillaume\documents\visual studio 2010\projects\sscce\sscce\main.cpp(48): or       'BasicE3OutStream<cT> &BasicE3OutStream<cT>::operator <<(std::string)'
1>          with
1>          [
1>              cT=char
1>          ]
1>          while trying to match the argument list '(E3OutStream, overloaded-function)'
1>c:\users\guillaume\documents\visual studio 2010\projects\sscce\sscce\main.cpp(132): error C2679: binary '<<' : no operator found which takes a right-hand operand of type 'overloaded-function' (or there is no acceptable conversion)
1>          c:\program files (x86)\microsoft visual studio 10.0\vc\include\ostream(679): could be 'std::basic_ostream<_Elem,_Traits> &std::operator <<<char,std::char_traits<char>>(std::basic_ostream<_Elem,_Traits> &,const char *)' [found using argument-dependent lookup]
1>          with
1>          [
1>              _Elem=char,
1>              _Traits=std::char_traits<char>
1>          ]
1>          c:\program files (x86)\microsoft visual studio 10.0\vc\include\ostream(726): or       'std::basic_ostream<_Elem,_Traits> &std::operator <<<char,std::char_traits<char>>(std::basic_ostream<_Elem,_Traits> &,char)' [found using argument-dependent lookup]
1>          with
1>          [
1>              _Elem=char,
1>              _Traits=std::char_traits<char>
1>          ]
1>          c:\program files (x86)\microsoft visual studio 10.0\vc\include\ostream(764): or       'std::basic_ostream<_Elem,_Traits> &std::operator <<<std::char_traits<char>>(std::basic_ostream<_Elem,_Traits> &,const char *)' [found using argument-dependent lookup]
1>          with
1>          [
1>              _Elem=char,
1>              _Traits=std::char_traits<char>
1>          ]
1>          c:\program files (x86)\microsoft visual studio 10.0\vc\include\ostream(811): or       'std::basic_ostream<_Elem,_Traits> &std::operator <<<std::char_traits<char>>(std::basic_ostream<_Elem,_Traits> &,char)' [found using argument-dependent lookup]
1>          with
1>          [
1>              _Elem=char,
1>              _Traits=std::char_traits<char>
1>          ]
1>          c:\program files (x86)\microsoft visual studio 10.0\vc\include\ostream(937): or       'std::basic_ostream<_Elem,_Traits> &std::operator <<<std::char_traits<char>>(std::basic_ostream<_Elem,_Traits> &,const signed char *)' [found using argument-dependent lookup]
1>          with
1>          [
1>              _Elem=char,
1>              _Traits=std::char_traits<char>
1>          ]
1>          c:\program files (x86)\microsoft visual studio 10.0\vc\include\ostream(944): or       'std::basic_ostream<_Elem,_Traits> &std::operator <<<std::char_traits<char>>(std::basic_ostream<_Elem,_Traits> &,signed char)' [found using argument-dependent lookup]
1>          with
1>          [
1>              _Elem=char,
1>              _Traits=std::char_traits<char>
1>          ]
1>          c:\program files (x86)\microsoft visual studio 10.0\vc\include\ostream(951): or       'std::basic_ostream<_Elem,_Traits> &std::operator <<<std::char_traits<char>>(std::basic_ostream<_Elem,_Traits> &,const unsigned char *)' [found using argument-dependent lookup]
1>          with
1>          [
1>              _Elem=char,
1>              _Traits=std::char_traits<char>
1>          ]
1>          c:\program files (x86)\microsoft visual studio 10.0\vc\include\ostream(958): or       'std::basic_ostream<_Elem,_Traits> &std::operator <<<std::char_traits<char>>(std::basic_ostream<_Elem,_Traits> &,unsigned char)' [found using argument-dependent lookup]
1>          with
1>          [
1>              _Elem=char,
1>              _Traits=std::char_traits<char>
1>          ]
1>          c:\program files (x86)\microsoft visual studio 10.0\vc\include\ostream(1085): or       'std::basic_ostream<_Elem,_Traits> &std::operator <<<char,std::char_traits<char>>(std::basic_ostream<_Elem,_Traits> &,const std::error_code &)' [found using argument-dependent lookup]
1>          with
1>          [
1>              _Elem=char,
1>              _Traits=std::char_traits<char>
1>          ]
1>          c:\users\guillaume\documents\visual studio 2010\projects\sscce\sscce\main.cpp(43): or       'BasicE3OutStream<cT> &BasicE3OutStream<cT>::operator <<(char *)'
1>          with
1>          [
1>              cT=char
1>          ]
1>          c:\users\guillaume\documents\visual studio 2010\projects\sscce\sscce\main.cpp(48): or       'BasicE3OutStream<cT> &BasicE3OutStream<cT>::operator <<(std::string)'
1>          with
1>          [
1>              cT=char
1>          ]
1>          while trying to match the argument list '(E3OutStream, overloaded-function)'
========== Build: 0 succeeded, 1 failed, 0 up-to-date, 0 skipped ==========

任何帮助将不胜感激, 纪尧姆

[编辑]

多亏了这个建议,我才意识到扩展 iostream 在那种情况下没有用,因为我已经有一个 ofstream 作为成员变量。由于我对 std 库的内部工作了解不够,我让我的类独立并且它在 MSVC 和 g++ 上工作:

#include <iostream>
#include <malloc.h>
#include <sstream>
#include <fstream>

class BasicE3OutStream {
    public:
        BasicE3OutStream() :
                mBuffer() {
            resetModifiers();
            mLineBuffer=(std::stringstream*) malloc(sizeof(std::stringstream));
            mLineBuffer=new (mLineBuffer) std::stringstream();
        }

        BasicE3OutStream(const BasicE3OutStream &) {
        }

        virtual ~BasicE3OutStream() {
            delete mLineBuffer;
            mLineBuffer=NULL;
        }

        void resetModifiers() {
            mDebug=false;
            mMemory=false;
        }

        void open(const char* fileName) {
            mBuffer.open(fileName, std::stringstream::out);
        }

        void close() {
            mBuffer.close();
        }

        template<class T>
        BasicE3OutStream& operator<<(T val) {
            (*mLineBuffer)<<val;
            return (*this);
        }

        void addLineFeed() {
            std::string modifier;
            modifier="";
            if (mDebug)
                modifier+="[DEBUG] ";
            else if (mMemory)
                modifier+="[MEMORY] ";

            mBuffer<<modifier<<mLineBuffer->str()<<"\n";
            mLineBuffer->str("");
            resetModifiers();
        }

        void flush() {
            mBuffer.flush();
            resetModifiers();
        }

        void setFlag(int f) {
            if (f==0)
                mDebug=true;
            else if (f==1)
                mMemory=true;
        }
    public:
        std::ofstream mBuffer;
        std::stringstream* mLineBuffer;
        bool mDebug, mMemory;
};

BasicE3OutStream& endl(BasicE3OutStream& os) {
    os.addLineFeed();
    return (os);
}

BasicE3OutStream& flush(BasicE3OutStream& os) {
    os.flush();
    return (os);
}

BasicE3OutStream& debug(BasicE3OutStream& os) {
    os.setFlag(0);
    return (os);
}

BasicE3OutStream& memory(BasicE3OutStream& os) {
    os.setFlag(1);
    return (os);
}

/**
 * Io manipulator, allows to use endl and other modificators
 */
BasicE3OutStream& operator<<(
        BasicE3OutStream &s
        , BasicE3OutStream& (*iomanip)(BasicE3OutStream&)) {
    return (iomanip(s));
}


typedef BasicE3OutStream E3OutStream;

int main() {
    E3OutStream s;
    s.open("output.txt");
    s<<debug<<"Debug info"<<endl;
    s<<flush;
    s<<memory<<"Memory info"<<endl;
    s<<flush;
    s.close();
}

最佳答案

问题的核心是您将函数指针发送到您的流,GCC 似乎知道如何处理,但 MSVC++ 不知道。不管怎样,我怀疑你想保存 debug 的地址/endl/flush/memory功能。

下一步的解决方案是制作那些对象,而不是您显示的函数。

但是,这段代码有一个很多错误。即,已经存在endlflush你应该使用的对象。此外,每个 BasicE3OutStream三个 底层流,它继承自一个,所有函数都在其上工作,(包括 endl 等等,我想这就是你替换它们的原因),和 mBuffermLineBuffer ,(您正在做的事情都不需要任何成员)。 (另一方面,用 mLineBuffer.str(""); 重置字符串流,而不是新的位置)

做你想做的事情的简单方法是重载每个 operator<<成员basic_ostream , 在有换行符的地方插入 [DEBUG][MEMORY]视情况而定,无需缓冲。 (我已经做到了,这并不难)

可能有一种正确的方法可以通过使用具有不同字符特征的流,或者重载某些虚函数,或者替换缓冲区来实现这一点,但是流很复杂,我不知道这个正确方式。

关于c++ - MSVC++ 扩展 iostream 时出错,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/9398391/

相关文章:

c++ - 用新的小部件替换 QTabWidget 的页面

perl - 从文本文件中持续读取数据的有效方法

c++ - 我如何在 wxWidgets 中重定向标准输入(istream)?

c++ - 测试 istream 对象

c++ - 可以阻止 cin 等待输入吗?

php - Base58 编码 Peercoin 公钥的步骤

c++ - boost 短字符串的轻量级

c++ - 如何正确地将 SHA1CryptoServiceProvider 转换为 C++?

Python、py.test 和 stderr——从 Cement 日志扩展中捕获日志处理程序输出

sql-server - 监视 SQL Server 2005 的帐户事件