3
votes

I have the following code:

namespace foo {
namespace bar {
class Baz {
protected:
    void print(std::ostream&);
public:
    friend std::ostream& operator<<(std::ostream& o, Baz& b) {
        b.print(o);
        return o;
    }
}}}

Then in another file:

Baz* b = getBaz();
LOG4CXX_INFO(logger, "Baz is " << *b);

I receive an error from gcc with the following message:

error: cannot bind 'std::basic_ostream<char>' lvalue to 'std::basic_ostream<char>&&'

It seems the confusion is because of the definition of this overload in log4cxx

// messagebuffer.h
template<class V>
std::basic_ostream<char>& operator<<(CharMessageBuffer& os, const V& val) {
    return ((std::basic_ostream<char>&) os) << val;
}

I tried to modify my code in the following way:

//forward declaration
namespace foo {
namespace bar {
class Baz;
}
}

namespace std {
using foo::bar::Baz;
//implementation in cpp file
std::ostream& operator<<(std::ostream& o, Baz& b);
}

namespace foo {
namespace bar {
class Baz {
protected:
    void print(std::ostream&);
public:
    friend std::ostream& std::operator<<(std::ostream& o, Baz& b);
}}}

However the code fails again with the same error. How can I force compiler to use my own version of the operator?

1

1 Answers

2
votes

It seems that you should declare the Baz& parameter as const Baz& and the print method should also be declared as const.

namespace foo {
namespace bar {

class Baz {
protected:
    void print(std::ostream&) const;
public:
    friend std::ostream& operator<<(std::ostream& o, const Baz& b) {
        b.print(o);
        return o;
    }
};

}}