overloaded operator << on ofstream concatenation problems

后端 未结 1 1663
广开言路
广开言路 2021-01-03 00:55

I have the following code:

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

std::ofstream &         


        
相关标签:
1条回答
  • 2021-01-03 01:12

    On the line :

    f << "label: " << s;
    

    Because the first call to operator<< returns a std::ostream &, the second fails to compile : the left operand to the operator is not of type std::ofstream anymore and your overload is not found.

    You should really use the second signature, as I see no reason for restricting your type to be outputted to std::ofstream.

    0 讨论(0)
提交回复
热议问题