Ostringstream user class output error

I don’t know why this is a mistake, but I'm just trying to add something akin to endl so that I can throw what is in the ostringstream for our debugger. I have the following:

class debug_stream_info
{
public:
    debug_stream_info(int errorLine, char *errorFile, int level)
        :m_errorLine(errorLine), m_errorFile(errorFile), m_logLevel(level)
    {
    }

    friend std::basic_ostringstream<char>& operator<<(std::basic_ostringstream<char>& os, debug_stream_info& debug_info);

private:
    int m_errorLine;
    std::string m_errorFile;
    int m_logLevel;

};

std::basic_ostringstream<char>& operator<<(std::basic_ostringstream<char>& os, debug_stream_info& debug_info)
{
    // Write the stream contents to cpu_debug
    // Deleted custom logging function.  No errors here though

    // Clear the stream for re-use.
    os.str("");
    os.seekp(0);

    return os;
}

int main(int argc, char** argv)
{
    std::ostringstream myout;
    myout << "hey there" << " and some more " << "Numbers!!: " << 435 << 54.2 << " that good for numbers" << debug_stream_info(__LINE__, __FILE__, LOG_LEVEL);

    return 0;
}

The error I get: error C2679: binary '<<' : no operator found which takes a right-hand operand of type 'debug_stream_info' (or there is no acceptable conversion)for the main line. This is on VS2008.

I include sstream, iostream, etc., and I have the correct namespaces. I have no other errors. I even tried replacing all the entries basic_ostreamwith simple ostringstream, and there was no difference (I will have a version w_charlater, but I wanted the simple case to work first). I made the object on the line above, and then passed the fully constructed object on the line, and the error was exactly the same. I changed the signature of the second argument to and from constwithout change.

, ?

: , , , std:: ostream, , std::ostringstream ( std::basic_ostringstream), . , ostream , os.str(), Ostream, .

+1
3

, std::ostringstream, std::ostream. , , :

debug_stream_info info(/** blah blah**/);

std::ostringstream oss;
oss << info ; //OK

:

oss << 1 << info; //ERROR

, oss<<1 std::ostream&, , debug_stream_info . , cast :

static_cast<std::ostringstream&>(oss << 1) << info; //OK

.

, , std::ostream std::basic_ostringstream.

, const &. .

, :

std::ostream& operator<<(std::ostream&, debug_stream_info const &);
                                                        //^^^^^^^ note this

const &, .

+7

debug_stream_info(__LINE__, __FILE__, LOG_LEVEL); , ,

#include <iostream>
#include <cstdio>
#include <sstream>
using namespace std;

class debug_stream_info
{
public:
    debug_stream_info(int errorLine, char *errorFile, int level)
        :m_errorLine(errorLine), m_errorFile(errorFile), m_logLevel(level)
    {
    }

    friend std::basic_ostringstream<char>& operator<<(std::basic_ostringstream<char>& os, debug_stream_info& debug_info);
    std::ostringstream& fun(std::ostringstream& os)
    {
        os<<"Ashish"<<endl;
        return os;
    }
private:
    int m_errorLine;
    std::string m_errorFile;
    int m_logLevel;

};

std::basic_ostringstream<char>& operator<<(std::basic_ostringstream<char>& os, debug_stream_info& debug_info)
{
    // Write the stream contents to cpu_debug
    // Deleted custom logging function.  No errors here though

    // Clear the stream for re-use.
//    os.str("");
//    os.seekp(0);

    return os;
}

int main(int argc, char** argv)
{

    std::ostringstream myout, test;
        myout << "hey there" << " and some more " << "Numbers!!: " << 435 << 54.2 << " that good for numbers"
         << debug_stream_info(1, "/home/ashish/test", 1).fun(test);
    return 0;
}
+1

Nawaz , . , std::istream. - :

class DebugStream
{
    std::ostringstring* collector;

public:
    template <typename T>
    DebugStream& operator<<( T const& value )
    {
        if ( collector != NULL ) {
            *collector << value;
        }
        return *this;
    }
};

; - - ; , , :

DebugStream( int lineNumber, std::string const& filename, int logLevel )
    : collector( isActive( logLevel ) ? new std::ostringstream : NULL )
{
    //  Initial insertion of lineNumber, filename, timestamp...
}

, ( , , ). ( . , , .)

, streambuf, stringstream. , . , new , , ( ).

+1
source

All Articles