c++ overload "<<" doesn't work for output "obja + objb"

c++ overload "<<" doesn't work for output "obja + objb"

I overload "<<" and "+" operators for my class. It works fine if I output single object using cout<<a;. But it fails when I output cout<<a+b;

g++ version: g++ (GCC) 9.2.0

class UTF8string{
    private:
        string data;
    public:
        UTF8string();
        UTF8string(string a);
        int bytes();
        int find(string a);
        int length();
        void replace(string a, string b);
        UTF8string operator+(UTF8string &a);
        UTF8string operator*(int a);
        UTF8string operator+=(const UTF8string& rhs){
            data.append(rhs.data);
            return *this;
        }
        friend UTF8string operator!(UTF8string &a);
        friend std::ostream& operator<<(std::ostream& os, UTF8string &a);
        friend UTF8string operator+(int a,  UTF8string &other );
};
std::ostream& operator<<(std::ostream& os, UTF8string &a){
    os << a.data;
    return os;
}
UTF8string UTF8string::operator+(UTF8string &a){
    string b = data ;
    return UTF8string(b.append(a.data));

}

my test code:

    cout << "test + test2: " << test + test2 << endl; // doesn't work
    cout << "test contains: " << test << endl; // works well

Error

testUTF8string.cpp:38:38: 错误:cannot bind non-const lvalue reference of type ‘UTF8string&’ to an rvalue of type ‘UTF8string’  
   38 |     cout << "test + test2: " << test + test2 << endl;  
      |                                 ~~~~~^~~~~~~

You're using references-to-non-const throughout.

That is a bad idea.

If you don't need to modify the object through a reference, the reference should be to const.

If your reference is not to const, temporaries cannot bind to it.

The return-by-value result of your operator+ is a temporary.

C Programming Tutorial for Beginners, Learn C the Hard Way: Practical Exercises on the Computational Subjects You Keep Avoiding (Like C) C or c is the third letter in the English and ISO basic Latin alphabets.Its name in English is cee (pronounced / ˈ s iː /), plural cees.


Result of your overloaded operator+ in test + test2 is temporary value. Temporary value cannot be passed to function by non-const reference.

It can be fixed by using const reference:

std::ostream& operator<<(std::ostream& os, const UTF8string &a){
    os << a.data;
    return os;
}

UTF8string UTF8string::operator+(const UTF8string &a){
    string b = data ;
    return UTF8string(b.append(a.data));
}

It is good practice to always use const references when passing arguments which are meant to be read-only.

"C" Programming Language: Brian Kernighan, This course will give you a full introduction into all of the core concepts in the C programming Duration: 3:46:13 Posted: Aug 15, 2018 Stock analysis for Citigroup Inc (C:New York) including stock price, stock chart, company news, key statistics, fundamentals and company profile.


Apart from the solution what @Lightness Races with Monica have suggested you can also overload the operator << for rvalue references like:-

friend std::ostream& operator<<(std::ostream& os, const UTF8string &&a);

It is sometimes useful to overload like this when moving is cheaper then copying. Or for some reason you want a different implementation in two cases.

NOTE: In case of operator << it doesn't make sense to do the oveloading as the data/object being passed to stream should be const

C Tutorial, "C" is one of the most widely used programming languages of all time. Prof Brian Kernighan Duration: 8:26 Posted: Aug 18, 2015 C is a procedural programming language. It was initially developed by Dennis Ritchie as a system programming language to write operating system. The main features of C language include low-level access to memory, simple set of keywords, and clean style, these features make C language suitable for system programming like operating system or compiler development.


Learn C, C programming is a general-purpose, procedural, imperative computer programming language developed in 1972 by Dennis M. Ritchie at the Bell Telephone� This is a list of operators in the C and C++ programming languages.All the operators listed exist in C++; the fourth column "Included in C", states whether an operator is also present in C. Note that C does not support operator overloading.


Learn C Programming, learn-c.org is a free interactive C tutorial for people who want to learn C, fast. As well as C and Simula's influences, other languages also influenced this new language, including ALGOL 68, Ada, CLU and ML . Initially, Stroustrup's "C with Classes" added features to the C compiler, Cpre, including classes, derived classes, strong typing, inlining and default arguments.


Cprogramming.com: Learn C and C++ Programming, C is a powerful general-purpose programming language. Our C tutorials will guide you to learn C programming one step at a time with the help of examples. Programming Languages Development - C++ has been used extensively in developing new programming languages like C#, Java, JavaScript, Perl, UNIX’s C Shell, PHP and Python, and Verilog etc. Computation Programming - C++ is the best friends of scientists because of fast speed and computational efficiencies.