Overloaded operator << on thread concatenation issues

I have the following code:

struct simple
{
    simple (int a1, int a2) : member1(a1), member2(a2) {}
    int member1;
    int member2;
};

std::ofstream &operator << (std::ofstream &f, const simple &obj)
{
    f<<obj.member1<<", "<<obj.member2;
    return f;
} 
int main(int argc, const char *argv[])
{
    std::ofstream f("streamout.txt");

    simple s(7,5);
    f << s;               //#1 This works
    f << "label: " << s;  //#2 This fails

    return 0;
}

I am trying to understand why # 1 works, while there are problems when trying to use an overloaded statement that concatenates it, as in # 2, which crashes with the following error (gcc 4.5.3 on MacOSX):

: 'std:: basic_ostream' lvalue ':: basic_ostream & &' /GCC - FACTORY/4.5/INSTALL/lib/gcc/x86_64-apple-darwin10.5.0/4.5.3/../../../../include/c++/4.5.3/ostream:579: 5: error: 1 'std:: basic_ostream < _CharT, _Traits > & :: < < (:: basic_ostream < _CharT, _Traits > &, const _Tp &) [ _CharT = char, _Traits = std:: char_traits, _Tp = simple] '

,

std::ostream &operator << (std::ostream &f, const simple &obj)
{ ... }

-, , - , ( const char * ") , " , . , .

+5
1

:

f << "label: " << s;

operator<< a std::ostream &, : std::ofstream, .

, std::ofstream.

+16

All Articles