operator overloading Error: cannot bind non-const lvalue reference of type ‘some-type’ to an rvalue of type ‘some-type’

  addition, c++, operator-overloading
#include <iostream>
#include <string>

using namespace std;

namespace ComplexNumberNamespace
{
    typedef struct _Data
    {
        double real;
        double imag;
    }Data;

    class ComplexNumber
    {
    private:
        Data data{};
    public:
        ComplexNumber();
        ComplexNumber(float real, float imag);
        ComplexNumber(ComplexNumber&);
        string to_string();
        void operator = (ComplexNumber&);
        ComplexNumber operator + (ComplexNumber& rhs);
    };

    string ComplexNumber::to_string() {
        std::string str;
        str = std::to_string(data.real);
        str.append(" + i ");
        str.append(std::to_string(data.imag));

        return str;
    }

    ComplexNumber::ComplexNumber(ComplexNumber & rhs) {
        std::cout<<"copy constructor";
        data.real = rhs.data.real;
        data.imag = rhs.data.imag;
    }

    ComplexNumber::ComplexNumber() {
        data.real = 0.0;
        data.imag = 0.0;
    }

    void ComplexNumber::operator=(ComplexNumber &cplx) {
        std::cout<<"assignment operator";
        data.real = cplx.data.real;
        data.imag = cplx.data.imag;
    }

    ComplexNumber::ComplexNumber(float real, float imag) {
        data.real = real;
        data.imag = imag;
    }

    ComplexNumber ComplexNumber::operator+(ComplexNumber &rhs)
    {
        ComplexNumber temp(data.real + rhs.data.real, data.imag+rhs.data.imag);

        return temp;
    }
}

using namespace ComplexNumberNamespace;

int main()
{
    ComplexNumber c1(1, 2);

    ComplexNumber c2(4, 3);

    ComplexNumber sum = c1 + c2;

    std::cout<<sum.to_string();
}

Compiler Output

====================[ Build | all | Debug ]=====================================
"C:Program FilesJetBrainsCLion 2020.3.2bincmakewinbincmake.exe" --build C:UserspcsourcereposDoxygeninput_cpp_filescmake-build-debug --target all -- -j 3
Scanning dependencies of target input_cpp_files
[ 50%] Building CXX object CMakeFiles/input_cpp_files.dir/MyCppFile.cpp.obj
C:UserspcsourcereposDoxygeninput_cpp_filesMyCppFile.cpp: In function 'int main()':
C:UserspcsourcereposDoxygeninput_cpp_filesMyCppFile.cpp:79:28: error: cannot bind non-const lvalue reference of type 'ComplexNumberNamespace::ComplexNumber&' to an rvalue of type 'ComplexNumberNamespace::ComplexNumber'
     ComplexNumber sum = c1 + c2;
                         ~~~^~~~
C:UserspcsourcereposDoxygeninput_cpp_filesMyCppFile.cpp:40:5: note:   initializing argument 1 of 'ComplexNumberNamespace::ComplexNumber::ComplexNumber(ComplexNumberNamespace::ComplexNumber&)'
     ComplexNumber::ComplexNumber(ComplexNumber & rhs) {
     ^~~~~~~~~~~~~
mingw32-make.exe[2]: *** [CMakeFilesinput_cpp_files.dirbuild.make:83: CMakeFiles/input_cpp_files.dir/MyCppFile.cpp.obj] Error 1
mingw32-make.exe[1]: *** [CMakeFilesMakefile2:95: CMakeFiles/input_cpp_files.dir/all] Error 2
mingw32-make.exe: *** [Makefile:103: all] Error 2

Why is my source code generating this error?

What am I doing incorrectly?

How can I fix this?

Source: Windows Questions C++

LEAVE A COMMENT