C++ Operator overloaded using friend function. Attempt to add multiple objects failed











up vote
0
down vote

favorite












Why compiler shows 'error' in 2nd case? (I have given link to full program)
Why do I have to use const keyword?



1st case:



friend Complex operator + (const Complex &,const Complex &);

Complex c5 = c1+c2+c3+c4;


2nd case:



friend Complex operator + ( Complex &, Complex &); 

Complex c5 = c1+c2+c3+c4;


1st case Full Program - I get proper Output



2nd case Full Program - error: no match for 'operator+'










share|improve this question




























    up vote
    0
    down vote

    favorite












    Why compiler shows 'error' in 2nd case? (I have given link to full program)
    Why do I have to use const keyword?



    1st case:



    friend Complex operator + (const Complex &,const Complex &);

    Complex c5 = c1+c2+c3+c4;


    2nd case:



    friend Complex operator + ( Complex &, Complex &); 

    Complex c5 = c1+c2+c3+c4;


    1st case Full Program - I get proper Output



    2nd case Full Program - error: no match for 'operator+'










    share|improve this question


























      up vote
      0
      down vote

      favorite









      up vote
      0
      down vote

      favorite











      Why compiler shows 'error' in 2nd case? (I have given link to full program)
      Why do I have to use const keyword?



      1st case:



      friend Complex operator + (const Complex &,const Complex &);

      Complex c5 = c1+c2+c3+c4;


      2nd case:



      friend Complex operator + ( Complex &, Complex &); 

      Complex c5 = c1+c2+c3+c4;


      1st case Full Program - I get proper Output



      2nd case Full Program - error: no match for 'operator+'










      share|improve this question















      Why compiler shows 'error' in 2nd case? (I have given link to full program)
      Why do I have to use const keyword?



      1st case:



      friend Complex operator + (const Complex &,const Complex &);

      Complex c5 = c1+c2+c3+c4;


      2nd case:



      friend Complex operator + ( Complex &, Complex &); 

      Complex c5 = c1+c2+c3+c4;


      1st case Full Program - I get proper Output



      2nd case Full Program - error: no match for 'operator+'







      c++ operator-overloading overloading






      share|improve this question















      share|improve this question













      share|improve this question




      share|improve this question








      edited Nov 19 at 12:16

























      asked Nov 19 at 12:11









      Lelouch Yagami

      144




      144
























          3 Answers
          3






          active

          oldest

          votes

















          up vote
          2
          down vote













          Complex& will not bind to a temporary, Complex const& will.



          Each + returns a temporary.



          As a general rule, you want:



          friend Complex operator + (Complex,const Complex &);


          but here two const& will do.






          share|improve this answer




























            up vote
            1
            down vote













            Temporaries dont bind to non-const references. When you write this



            auto c3 = c2 + c1 + c0; 


            then first c1+c0 will be computed, and the result passed to c2.operator+(). When the operator is declared to take a Complex& then you cannot pass a temporary, when it takes const Complex& then you can. Passing a temporary when a non-const reference is expected is a logic error in 90% of the cases, hence it is forbidden.






            share|improve this answer






























              up vote
              1
              down vote













              The expression c1+c2+c3+c4 is parsed and evaluated as if



              Complex c5 = operator+(c1, operator+(c2, operator+(c3, c4)));


              operator+(c3, c4) builds and returns a temporary Complex object: an rvalue.



              C++ forbids an rvalue to be bound to a non-const lvalue-reference.



              But operator+(Complex&, Complex&) takes a non-const lvalue-reference. Hence the error message.



              On the other hand, operator+(Complex const&, Complex const&) takes references to const lvalues.






              share|improve this answer























                Your Answer






                StackExchange.ifUsing("editor", function () {
                StackExchange.using("externalEditor", function () {
                StackExchange.using("snippets", function () {
                StackExchange.snippets.init();
                });
                });
                }, "code-snippets");

                StackExchange.ready(function() {
                var channelOptions = {
                tags: "".split(" "),
                id: "1"
                };
                initTagRenderer("".split(" "), "".split(" "), channelOptions);

                StackExchange.using("externalEditor", function() {
                // Have to fire editor after snippets, if snippets enabled
                if (StackExchange.settings.snippets.snippetsEnabled) {
                StackExchange.using("snippets", function() {
                createEditor();
                });
                }
                else {
                createEditor();
                }
                });

                function createEditor() {
                StackExchange.prepareEditor({
                heartbeatType: 'answer',
                convertImagesToLinks: true,
                noModals: true,
                showLowRepImageUploadWarning: true,
                reputationToPostImages: 10,
                bindNavPrevention: true,
                postfix: "",
                imageUploader: {
                brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
                contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
                allowUrls: true
                },
                onDemand: true,
                discardSelector: ".discard-answer"
                ,immediatelyShowMarkdownHelp:true
                });


                }
                });














                 

                draft saved


                draft discarded


















                StackExchange.ready(
                function () {
                StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53374382%2fc-operator-overloaded-using-friend-function-attempt-to-add-multiple-objects-f%23new-answer', 'question_page');
                }
                );

                Post as a guest















                Required, but never shown

























                3 Answers
                3






                active

                oldest

                votes








                3 Answers
                3






                active

                oldest

                votes









                active

                oldest

                votes






                active

                oldest

                votes








                up vote
                2
                down vote













                Complex& will not bind to a temporary, Complex const& will.



                Each + returns a temporary.



                As a general rule, you want:



                friend Complex operator + (Complex,const Complex &);


                but here two const& will do.






                share|improve this answer

























                  up vote
                  2
                  down vote













                  Complex& will not bind to a temporary, Complex const& will.



                  Each + returns a temporary.



                  As a general rule, you want:



                  friend Complex operator + (Complex,const Complex &);


                  but here two const& will do.






                  share|improve this answer























                    up vote
                    2
                    down vote










                    up vote
                    2
                    down vote









                    Complex& will not bind to a temporary, Complex const& will.



                    Each + returns a temporary.



                    As a general rule, you want:



                    friend Complex operator + (Complex,const Complex &);


                    but here two const& will do.






                    share|improve this answer












                    Complex& will not bind to a temporary, Complex const& will.



                    Each + returns a temporary.



                    As a general rule, you want:



                    friend Complex operator + (Complex,const Complex &);


                    but here two const& will do.







                    share|improve this answer












                    share|improve this answer



                    share|improve this answer










                    answered Nov 19 at 12:23









                    Yakk - Adam Nevraumont

                    178k19185363




                    178k19185363
























                        up vote
                        1
                        down vote













                        Temporaries dont bind to non-const references. When you write this



                        auto c3 = c2 + c1 + c0; 


                        then first c1+c0 will be computed, and the result passed to c2.operator+(). When the operator is declared to take a Complex& then you cannot pass a temporary, when it takes const Complex& then you can. Passing a temporary when a non-const reference is expected is a logic error in 90% of the cases, hence it is forbidden.






                        share|improve this answer



























                          up vote
                          1
                          down vote













                          Temporaries dont bind to non-const references. When you write this



                          auto c3 = c2 + c1 + c0; 


                          then first c1+c0 will be computed, and the result passed to c2.operator+(). When the operator is declared to take a Complex& then you cannot pass a temporary, when it takes const Complex& then you can. Passing a temporary when a non-const reference is expected is a logic error in 90% of the cases, hence it is forbidden.






                          share|improve this answer

























                            up vote
                            1
                            down vote










                            up vote
                            1
                            down vote









                            Temporaries dont bind to non-const references. When you write this



                            auto c3 = c2 + c1 + c0; 


                            then first c1+c0 will be computed, and the result passed to c2.operator+(). When the operator is declared to take a Complex& then you cannot pass a temporary, when it takes const Complex& then you can. Passing a temporary when a non-const reference is expected is a logic error in 90% of the cases, hence it is forbidden.






                            share|improve this answer














                            Temporaries dont bind to non-const references. When you write this



                            auto c3 = c2 + c1 + c0; 


                            then first c1+c0 will be computed, and the result passed to c2.operator+(). When the operator is declared to take a Complex& then you cannot pass a temporary, when it takes const Complex& then you can. Passing a temporary when a non-const reference is expected is a logic error in 90% of the cases, hence it is forbidden.







                            share|improve this answer














                            share|improve this answer



                            share|improve this answer








                            edited Nov 19 at 12:34

























                            answered Nov 19 at 12:23









                            user463035818

                            15.7k42561




                            15.7k42561






















                                up vote
                                1
                                down vote













                                The expression c1+c2+c3+c4 is parsed and evaluated as if



                                Complex c5 = operator+(c1, operator+(c2, operator+(c3, c4)));


                                operator+(c3, c4) builds and returns a temporary Complex object: an rvalue.



                                C++ forbids an rvalue to be bound to a non-const lvalue-reference.



                                But operator+(Complex&, Complex&) takes a non-const lvalue-reference. Hence the error message.



                                On the other hand, operator+(Complex const&, Complex const&) takes references to const lvalues.






                                share|improve this answer



























                                  up vote
                                  1
                                  down vote













                                  The expression c1+c2+c3+c4 is parsed and evaluated as if



                                  Complex c5 = operator+(c1, operator+(c2, operator+(c3, c4)));


                                  operator+(c3, c4) builds and returns a temporary Complex object: an rvalue.



                                  C++ forbids an rvalue to be bound to a non-const lvalue-reference.



                                  But operator+(Complex&, Complex&) takes a non-const lvalue-reference. Hence the error message.



                                  On the other hand, operator+(Complex const&, Complex const&) takes references to const lvalues.






                                  share|improve this answer

























                                    up vote
                                    1
                                    down vote










                                    up vote
                                    1
                                    down vote









                                    The expression c1+c2+c3+c4 is parsed and evaluated as if



                                    Complex c5 = operator+(c1, operator+(c2, operator+(c3, c4)));


                                    operator+(c3, c4) builds and returns a temporary Complex object: an rvalue.



                                    C++ forbids an rvalue to be bound to a non-const lvalue-reference.



                                    But operator+(Complex&, Complex&) takes a non-const lvalue-reference. Hence the error message.



                                    On the other hand, operator+(Complex const&, Complex const&) takes references to const lvalues.






                                    share|improve this answer














                                    The expression c1+c2+c3+c4 is parsed and evaluated as if



                                    Complex c5 = operator+(c1, operator+(c2, operator+(c3, c4)));


                                    operator+(c3, c4) builds and returns a temporary Complex object: an rvalue.



                                    C++ forbids an rvalue to be bound to a non-const lvalue-reference.



                                    But operator+(Complex&, Complex&) takes a non-const lvalue-reference. Hence the error message.



                                    On the other hand, operator+(Complex const&, Complex const&) takes references to const lvalues.







                                    share|improve this answer














                                    share|improve this answer



                                    share|improve this answer








                                    edited 10 hours ago

























                                    answered Nov 19 at 12:23









                                    YSC

                                    19.5k34591




                                    19.5k34591






























                                         

                                        draft saved


                                        draft discarded



















































                                         


                                        draft saved


                                        draft discarded














                                        StackExchange.ready(
                                        function () {
                                        StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53374382%2fc-operator-overloaded-using-friend-function-attempt-to-add-multiple-objects-f%23new-answer', 'question_page');
                                        }
                                        );

                                        Post as a guest















                                        Required, but never shown





















































                                        Required, but never shown














                                        Required, but never shown












                                        Required, but never shown







                                        Required, but never shown

































                                        Required, but never shown














                                        Required, but never shown












                                        Required, but never shown







                                        Required, but never shown







                                        Popular posts from this blog

                                        MongoDB - Not Authorized To Execute Command

                                        How to fix TextFormField cause rebuild widget in Flutter

                                        in spring boot 2.1 many test slices are not allowed anymore due to multiple @BootstrapWith