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+'
c++ operator-overloading overloading
add a comment |
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+'
c++ operator-overloading overloading
add a comment |
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+'
c++ operator-overloading overloading
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
c++ operator-overloading overloading
edited Nov 19 at 12:16
asked Nov 19 at 12:11


Lelouch Yagami
144
144
add a comment |
add a comment |
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.
add a comment |
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.
add a comment |
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.
add a comment |
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.
add a comment |
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.
add a comment |
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.
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.
answered Nov 19 at 12:23
Yakk - Adam Nevraumont
178k19185363
178k19185363
add a comment |
add a comment |
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.
add a comment |
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.
add a comment |
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.
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.
edited Nov 19 at 12:34
answered Nov 19 at 12:23
user463035818
15.7k42561
15.7k42561
add a comment |
add a comment |
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.
add a comment |
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.
add a comment |
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.
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.
edited 10 hours ago
answered Nov 19 at 12:23


YSC
19.5k34591
19.5k34591
add a comment |
add a comment |
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
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
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
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