Question

J'écris une classe de gestion de sortie pour mon application et bien que ma classe fonctionne sur G ++ sous Linux, elle ne se compilera pas sous MSVC ++ 2010.

Voici une version SSCCE de mon programme:

#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();
}

Il fonctionne bien dans G ++ cependant sur MSVC, j'obtiens une erreur de compilateur:

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 ==========

Toute aide serait grandement appréciée, Guillaume

ÉDITER

Grâce à ce conseil qui a été donné, j'ai pu réaliser que l'extension d'iOStream n'était pas utile dans ce cas car j'avais déjà un Ofstream en tant que variable de membre. Comme je ne connais pas assez le travail interne de la bibliothèque STD, j'ai rendu ma classe indépendante et cela a fonctionné sur MSVC et 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();
}
Était-ce utile?

La solution

Le cœur du problème est que vous envoyez des pointeurs de fonction dans votre flux, ce que GCC semble savoir comment gérer, mais MSVC ++ ne le fait pas. Quoi qu'il en soit, je doute que vous vouliez sauver l'adresse du debug/endl/flush/memory les fonctions.

La résolution de la prochaine étape est de faire ces objets au lieu de fonctions que vous affichez.

Cependant, il y a un parcelle mal avec ce code. À savoir, il existe déjà endl et flush objets, que vous devriez utiliser. Aussi, chacun BasicE3OutStream a Trois des flux sous-jacents, dont il a hérité, sur lesquels toutes les fonctions fonctionnent (y compris endl Et tels, ce que je suppose, c'est pourquoi vous les avez remplacés), et mBuffer et mLineBuffer, (Aucun des membres n'est nécessaire pour ce que vous faites). (Une autre note latérale, réinitialisez une bande de cordes avec mLineBuffer.str("");, pas de placement nouveau)

Le moyen facile de faire ce que vous voulez est de surcharger chacun des operator<< membres d'un basic_ostream, et partout où il y a une nouvelle ligne, insérer [DEBUG] ou [MEMORY] Comme approprié, il n'est pas nécessaire de tamponner. (J'ai fait ça, ce n'est pas fou dur)

Il y a probablement un droit façon de le faire en utilisant un flux avec différents traits de char, ou en surchargeant certaines fonctions virtuelles, ou en remplacement du tampon, mais les flux sont compliqués, et je ne sais pas cela droit façon.

Licencié sous: CC-BY-SA avec attribution
Non affilié à StackOverflow
scroll top