473,406 Members | 2,217 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,406 software developers and data experts.

ambiguous overload

#include<iostream>
using namespace std;
class Integer{
int i;
public:
Integer(int ii):i(ii){}
const Integer operator+(const Integer& rv){
cout<<"1-operator+"<<endl;
return Integer(i+rv.i);
}
Integer operator+=(Integer rv){
i+=rv.i;
cout<<"2-operator+="<<endl;
return *this;
}
friend Integer& operator+(Integer,Integer);

void display(){
cout<<"3-display()// i="<<i<<endl;
}
};
Integer& operator+(Integer rv,Integer rs ){
cout<<"2-operator+="<<endl;
rv.i+=rs.i;
return rv;
}
int main(void){
cout<<"buit-in types"<<endl;
int i=1,j=2,k=3;
k+=i+j;
cout<<"user defined types:"<<endl;
Integer ii(1),jj(2),kk(3);
kk+=ii+jj;
Integer d(1),b(2);
d=2+b;
d.display();
return 0;
}


g++ -g -Wall -o test test.cpp
test.cpp: In function 'Integer& operator+(Integer, Integer)':
test.cpp:22: warning: reference to local variable 'rv' returned
test.cpp: In function 'int main()':
test.cpp:33: error: ambiguous overload for 'operator+' in 'ii + jj'
test.cpp:7: note: candidates are: const Integer Integer::operator+
(const Integer&)
test.cpp:22: note: Integer& operator+(Integer,
Integer)
make: *** [test] Error 1
??

What should I do to prevent this ??
Jan 7 '08 #1
11 4619
onkar wrote:
#include<iostream>
using namespace std;
class Integer{
int i;
public:
Integer(int ii):i(ii){}
const Integer operator+(const Integer& rv){
cout<<"1-operator+"<<endl;
return Integer(i+rv.i);
}
Integer operator+=(Integer rv){
i+=rv.i;
cout<<"2-operator+="<<endl;
return *this;
}
friend Integer& operator+(Integer,Integer);

void display(){
cout<<"3-display()// i="<<i<<endl;
}
};
Integer& operator+(Integer rv,Integer rs ){
cout<<"2-operator+="<<endl;
rv.i+=rs.i;
return rv;
You shouldn't return a reference to a local variable!
What should I do to prevent this ??
Stick to one operator+. Why have two?

--
Ian Collins.
Jan 7 '08 #2
On Jan 7, 11:02 am, Ian Collins <ian-n...@hotmail.comwrote:
onkar wrote:
#include<iostream>
using namespace std;
class Integer{
int i;
public:
Integer(int ii):i(ii){}
const Integer operator+(const Integer& rv){
cout<<"1-operator+"<<endl;
return Integer(i+rv.i);
}
Integer operator+=(Integer rv){
i+=rv.i;
cout<<"2-operator+="<<endl;
return *this;
}
friend Integer& operator+(Integer,Integer);
void display(){
cout<<"3-display()// i="<<i<<endl;
}
};
Integer& operator+(Integer rv,Integer rs ){
cout<<"2-operator+="<<endl;
rv.i+=rs.i;
return rv;

You shouldn't return a reference to a local variable!
What should I do to prevent this ??

Stick to one operator+. Why have two?

--
Ian Collins.
#include<iostream>
using namespace std;
class Integer{
int i;
public:
Integer(int ii):i(ii){}
/* const Integer operator+(const Integer& rv){
cout<<"1-operator+"<<endl;
return Integer(i+rv.i);
} */
Integer operator+=(Integer rv){
i+=rv.i;
cout<<"2-operator+="<<endl;
return *this;
}
friend Integer& operator+(Integer,Integer);

void display(){
cout<<"3-display()// i="<<i<<endl;
}
};
Integer& operator+(Integer rv,Integer rs ){
cout<<"4-operator+"<<endl;
rv.i+=rs.i;
return rv;
}
int main(void){
cout<<"buit-in types"<<endl;
int i=1,j=2,k=3;
k+=i+j;
cout<<"user defined types:"<<endl;
Integer ii(1),jj(2),kk(3);
kk+=ii+jj;
Integer d(1),b(2);
d=2+b;
d.display();
return 0;
}

How to prevent this warning ??
g++ -g -Wall -o test test.cpp
test.cpp: In function 'Integer& operator+(Integer, Integer)':
test.cpp:22: warning: reference to local variable 'rv' returned

Note: If I remove & in
Integer& operator+(Integer rv,Integer rs )

g++ -g -Wall -o test test.cpp
test.cpp: In function 'Integer operator+(Integer, Integer)':
test.cpp:22: error: new declaration 'Integer operator+(Integer,
Integer)'
test.cpp:16: error: ambiguates old declaration 'Integer& operator+
(Integer, Integer)'
make: *** [test] Error 1

?? deadlocklike situation ?? :(
Jan 7 '08 #3
onkar wrote:
On Jan 7, 11:02 am, Ian Collins <ian-n...@hotmail.comwrote:
>onkar wrote:
>>Integer& operator+(Integer rv,Integer rs ){
cout<<"2-operator+="<<endl;
rv.i+=rs.i;
return rv;
You shouldn't return a reference to a local variable!
>>What should I do to prevent this ??
Stick to one operator+. Why have two?
*Don't* quote signatures.
>
How to prevent this warning ??
I told you last time.

--
Ian Collins.
Jan 7 '08 #4
In article <a21174f2-a51d-43e0-ae68-ac11ab654101
@e23g2000prf.googlegroups.com>, on*******@gmail.com says...

[ ... ]
Integer& operator+(Integer rv,Integer rs ){
cout<<"4-operator+"<<endl;
rv.i+=rs.i;
return rv;
}
You're still returning a reference to a local variable. As much as you'd
like to return a reference to some existing variable, you really can't
-- you need to create and return a new instance. OTOH, you can pass one
of the parameters by reference (to const).

[ ... ]
Note: If I remove & in
Integer& operator+(Integer rv,Integer rs )

g++ -g -Wall -o test test.cpp
test.cpp: In function 'Integer operator+(Integer, Integer)':
test.cpp:22: error: new declaration 'Integer operator+(Integer,
Integer)'
test.cpp:16: error: ambiguates old declaration 'Integer& operator+
(Integer, Integer)'
The compiler is just telling you that this function body no longer
matches your friend declaration above, and the two (if there really were
two) would be ambiguous. You just need to make the friend declaration
and the function definition match each other. One way to do that is to
put them together:

class Integer {
// ...

friend Integer operator+(Integer rv, Integer rs) {
rv.i+=rs.i;
return rv;
}
};

Even though we've placed the body of the operator+ inside of the class
defintion, it is still a _global_ function, visible outside the class,
just as if its defintion were outside the class like you wrote it. FWIW,
this code is definitely open to further optimization (it creates at
least one more Integer object than necessary).

--
Later,
Jerry.

The universe is a figment of its own imagination.
Jan 7 '08 #5
On Jan 7, 1:07 am, onkar <onkar....@gmail.comwrote:
<snip>
onkar wrote:
How to prevent this warning ??

g++ -g -Wall -o test test.cpp
test.cpp: In function 'Integer& operator+(Integer, Integer)':
test.cpp:22: warning: reference to local variable 'rv' returned
To prevent the warning, listen to the compiler. Acknowledge that rv, a
local variable, is destroyed once the function returns. You'll end up
with an invalid reference. One doesn't mail a letter to an address
that is about to cease to exist. Sounds silly because it is silly.

According to the statements you are showing in main(),
a) the only friend op i see would be:

friend Integer operator+(const int, const Integer&);

b) and a member function to add a primitive integer:

Integer& Integer::operator+(const int n)
{
...
}

Jan 7 '08 #6
On Jan 7, 7:43 am, onkar <onkar....@gmail.comwrote:
#include<iostream>
using namespace std;
class Integer{
int i;
public:
Integer(int ii):i(ii){}
const Integer operator+(const Integer& rv){
cout<<"1-operator+"<<endl;
return Integer(i+rv.i);
}
Integer operator+=(Integer rv){
i+=rv.i;
cout<<"2-operator+="<<endl;
return *this;
}
friend Integer& operator+(Integer,Integer);

void display(){
cout<<"3-display()// i="<<i<<endl;
}};

Integer& operator+(Integer rv,Integer rs ){
cout<<"2-operator+="<<endl;
rv.i+=rs.i;
return rv;}

int main(void){
cout<<"buit-in types"<<endl;
int i=1,j=2,k=3;
k+=i+j;
cout<<"user defined types:"<<endl;
Integer ii(1),jj(2),kk(3);
kk+=ii+jj;
Integer d(1),b(2);
d=2+b;
d.display();
return 0;

}

g++ -g -Wall -o test test.cpp
test.cpp: In function 'Integer& operator+(Integer, Integer)':
test.cpp:22: warning: reference to local variable 'rv' returned
test.cpp: In function 'int main()':
test.cpp:33: error: ambiguous overload for 'operator+' in 'ii + jj'
test.cpp:7: note: candidates are: const Integer Integer::operator+
(const Integer&)
test.cpp:22: note: Integer& operator+(Integer,
Integer)
make: *** [test] Error 1
??

What should I do to prevent this ??
For classes like the one designed by you, often is a good idea to
overload a conversion operator like "operator int();".
That way, you don't need to overload all the needed arithmetic
operators, relational operators, ...
Jan 7 '08 #7
On Jan 7, 8:35*am, asterisc <Rares....@ni.comwrote:
On Jan 7, 7:43 am, onkar <onkar....@gmail.comwrote:


#include<iostream>
using namespace std;
class Integer{
* * * * int i;
* * * * public:
* * * * * * * * Integer(int ii):i(ii){}
* * * * * * * * const Integer operator+(const Integer& rv){
* * * * * * * * * * * * cout<<"1-operator+"<<endl;
* * * * * * * * * * * * return Integer(i+rv.i);
* * * * * * * * }
* * * * * * * * Integer operator+=(Integer rv){
* * * * * * * * * * * * i+=rv.i;
* * * * * * * * * * * * cout<<"2-operator+="<<endl;
* * * * * * * * * * * * return *this;
* * * * * * * * }
* * * * * * * * friend Integer& operator+(Integer,Integer);
* * * * * * * * void display(){
* * * * * * * * * * * * cout<<"3-display()// i="<<i<<endl;
* * * * * * * * }};
Integer& operator+(Integer rv,Integer rs ){
* * * * * * * * * * * * cout<<"2-operator+="<<endl;
* * * * rv.i+=rs.i;
* * * * return rv;}
int main(void){
* * * * *cout<<"buit-in types"<<endl;
* * * * int i=1,j=2,k=3;
* * * * k+=i+j;
* * * * cout<<"user defined types:"<<endl;
* * * * Integer ii(1),jj(2),kk(3);
* * * * kk+=ii+jj;
* * * * Integer d(1),b(2);
* * * * d=2+b;
* * * * d.display();
* * * * return 0;
}
g++ -g -Wall -o test test.cpp
test.cpp: In function 'Integer& operator+(Integer, Integer)':
test.cpp:22: warning: reference to local variable 'rv' returned
test.cpp: In function 'int main()':
test.cpp:33: error: ambiguous overload for 'operator+' in 'ii + jj'
test.cpp:7: note: candidates are: const Integer Integer::operator+
(const Integer&)
test.cpp:22: note: * * * * * * * * Integer& operator+(Integer,
Integer)
make: *** [test] Error 1
??
What should I do to prevent this ??

For classes like the one designed by you, often is a good idea to
overload a conversion operator like "operator int();".
That way, you don't need to overload all the needed arithmetic
operators, relational operators, ...- Hide quoted text -

- Show quoted text -
Better way to have only a friend operator+ function. Do not have
member fn. This way you can also do calcultions like
Integer I(1);
Integer J = 1 + I;

operator+ can be implemented in terms of operator+=. e.g.

friend const Integer(const Integer& I1,const Integer& I2)
{
Integer temp(I1);
temp += I2;
return temp;
}
Jan 7 '08 #8
siddhu wrote:
On Jan 7, 8:35 am, asterisc <Rares....@ni.comwrote:
>On Jan 7, 7:43 am, onkar <onkar....@gmail.comwrote:


>>#include<iostream>
using namespace std;
class Integer{
int i;
public:
Integer(int ii):i(ii){}
const Integer operator+(const Integer& rv){
cout<<"1-operator+"<<endl;
return Integer(i+rv.i);
}
Integer operator+=(Integer rv){
i+=rv.i;
cout<<"2-operator+="<<endl;
return *this;
}
friend Integer& operator+(Integer,Integer);
>>void display(){
cout<<"3-display()// i="<<i<<endl;
}};
>>Integer& operator+(Integer rv,Integer rs ){
cout<<"2-operator+="<<endl;
rv.i+=rs.i;
return rv;}
>>int main(void){
cout<<"buit-in types"<<endl;
int i=1,j=2,k=3;
k+=i+j;
cout<<"user defined types:"<<endl;
Integer ii(1),jj(2),kk(3);
kk+=ii+jj;
Integer d(1),b(2);
d=2+b;
d.display();
return 0;
>>}
>>g++ -g -Wall -o test test.cpp
test.cpp: In function 'Integer& operator+(Integer, Integer)':
test.cpp:22: warning: reference to local variable 'rv' returned
test.cpp: In function 'int main()':
test.cpp:33: error: ambiguous overload for 'operator+' in 'ii + jj'
test.cpp:7: note: candidates are: const Integer Integer::operator+
(const Integer&)
test.cpp:22: note: Integer& operator+(Integer,
Integer)
make: *** [test] Error 1
??
>>What should I do to prevent this ??

For classes like the one designed by you, often is a good idea to
overload a conversion operator like "operator int();".
That way, you don't need to overload all the needed arithmetic
operators, relational operators, ...- Hide quoted text -

- Show quoted text -

Better way to have only a friend operator+ function. Do not have
member fn. This way you can also do calcultions like
Integer I(1);
Integer J = 1 + I;

operator+ can be implemented in terms of operator+=. e.g.

friend const Integer(const Integer& I1,const Integer& I2)
Probably meant to define it as

friend const Integer operator+(const Integer ....

although I am not sure why you want it to return a 'const'.
{
Integer temp(I1);
temp += I2;
return temp;
}
And the body could simply look like this

{
return Integer(I1.i + I2.i);
}

There is really no need to implement it in terms of +=, is there?

V
--
Please remove capital 'A's when replying by e-mail
I do not respond to top-posted replies, please don't ask
Jan 7 '08 #9
On Jan 7, 7:07 am, onkar <onkar....@gmail.comwrote:
#include<iostream>
using namespace std;
class Integer{
int i;
public:
Integer(int ii):i(ii){}
/* const Integer operator+(const Integer& rv){
cout<<"1-operator+"<<endl;
return Integer(i+rv.i);
} */
Integer operator+=(Integer rv){
i+=rv.i;
cout<<"2-operator+="<<endl;
return *this;
}
friend Integer& operator+(Integer,Integer);

void display(){
cout<<"3-display()// i="<<i<<endl;
}};
Integer& operator+(Integer rv,Integer rs ){
cout<<"4-operator+"<<endl;
rv.i+=rs.i;
return rv;}
int main(void){
cout<<"buit-in types"<<endl;
int i=1,j=2,k=3;
k+=i+j;
cout<<"user defined types:"<<endl;
Integer ii(1),jj(2),kk(3);
kk+=ii+jj;
Integer d(1),b(2);
d=2+b;
d.display();
return 0;
}
How to prevent this warning ??
g++ -g -Wall -o test test.cpp
test.cpp: In function 'Integer& operator+(Integer, Integer)':
test.cpp:22: warning: reference to local variable 'rv' returned
Read the text of the warning. It's quite clear. The obvious
way to avoid a warning "reference to local variable 'rv'
returned" is to not return a reference to the local variable
'rv', no?

In general, you shouldn't ever return a reference to a local
variable, since there's no way the client can use that reference
without incurring undefined behavior.
Note: If I remove & in
Integer& operator+(Integer rv,Integer rs )
g++ -g -Wall -o test test.cpp
test.cpp: In function 'Integer operator+(Integer, Integer)':
test.cpp:22: error: new declaration 'Integer operator+(Integer,
Integer)'
test.cpp:16: error: ambiguates old declaration 'Integer& operator+
(Integer, Integer)'
make: *** [test] Error 1
Of course. All of the declarations have to be identical. If
you remove it in one declaration, you have to remove it in all.

--
James Kanze (GABI Software) email:ja*********@gmail.com
Conseils en informatique orientée objet/
Beratung in objektorientierter Datenverarbeitung
9 place Sémard, 78210 St.-Cyr-l'École, France, +33 (0)1 30 23 00 34
Jan 8 '08 #10
On Jan 7, 2:35 pm, asterisc <Rares....@ni.comwrote:
On Jan 7, 7:43 am, onkar <onkar....@gmail.comwrote:
#include<iostream>
using namespace std;
class Integer{
int i;
public:
Integer(int ii):i(ii){}
const Integer operator+(const Integer& rv){
cout<<"1-operator+"<<endl;
return Integer(i+rv.i);
}
Integer operator+=(Integer rv){
i+=rv.i;
cout<<"2-operator+="<<endl;
return *this;
}
friend Integer& operator+(Integer,Integer);
void display(){
cout<<"3-display()// i="<<i<<endl;
}};
Integer& operator+(Integer rv,Integer rs ){
cout<<"2-operator+="<<endl;
rv.i+=rs.i;
return rv;}
int main(void){
cout<<"buit-in types"<<endl;
int i=1,j=2,k=3;
k+=i+j;
cout<<"user defined types:"<<endl;
Integer ii(1),jj(2),kk(3);
kk+=ii+jj;
Integer d(1),b(2);
d=2+b;
d.display();
return 0;
}
g++ -g -Wall -o test test.cpp
test.cpp: In function 'Integer& operator+(Integer, Integer)':
test.cpp:22: warning: reference to local variable 'rv' returned
test.cpp: In function 'int main()':
test.cpp:33: error: ambiguous overload for 'operator+' in 'ii + jj'
test.cpp:7: note: candidates are: const Integer Integer::operator+
(const Integer&)
test.cpp:22: note: Integer& operator+(Integer,
Integer)
make: *** [test] Error 1
??
What should I do to prevent this ??

For classes like the one designed by you, often is a good idea to
overload a conversion operator like "operator int();".
That way, you don't need to overload all the needed arithmetic
operators, relational operators, ...
Jan 8 '08 #11
On Jan 7, 5:51 pm, "Victor Bazarov" <v.Abaza...@comAcast.netwrote:
siddhu wrote:
[...]
Better way to have only a friend operator+ function. Do not have
member fn. This way you can also do calcultions like
Integer I(1);
Integer J = 1 + I;
operator+ can be implemented in terms of operator+=. e.g.
friend const Integer(const Integer& I1,const Integer& I2)
Probably meant to define it as
friend const Integer operator+(const Integer ....
although I am not sure why you want it to return a 'const'.
To prevent things like:
Integer i, j, k ;
(i + j) = k ;
I've read (in Scott Meyers, I think) that this is a good idea.
Abstractly, I think it's a good idea. In practice, I've never
bothered; the type of error it prevents doesn't seem to occur in
practice anyway.
{
Integer temp(I1);
temp += I2;
return temp;
}
And the body could simply look like this
{
return Integer(I1.i + I2.i);
}
There is really no need to implement it in terms of +=, is there?
In such a simple case as this, no. It's the idiomatic solution,
however---normally, if you're doing much of this sort of thing,
you'll actually defined such operations in a templated base
class, so all you have to do is derive, e.g.:

template< typename T >
class ArithmeticOperators
{
friend T operator+( T const& lhs, T const& rhs )
{
T result( lhs ) ;
result += rhs ;
return result ;
}
// The same for all of the arithmeitc operators...
} ;

class BigInteger : private ArithmeticOperators< BigInteger >
{
public :
// ...
BigInteger& operator+=( BigInteger const& other ) ;
// ...
} ;

It saves a lot of typing, and positively guarantees that + will
have the same semantics as +=. (And if you've misunderstood the
requirements, and implemented the wrong semantics, you only have
to change the code at one place.)

--
James Kanze (GABI Software) email:ja*********@gmail.com
Conseils en informatique orientée objet/
Beratung in objektorientierter Datenverarbeitung
9 place Sémard, 78210 St.-Cyr-l'École, France, +33 (0)1 30 23 00 34
Jan 8 '08 #12

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

1
by: Alan Johnson | last post by:
Consider the following code, with the interesting lines numbered in comments: class A { public : bool f(int level = 1) // line 5 { return true ; }
0
by: Michi Henning | last post by:
Hi, the following code produces an error on the second-last line: Interface Left Sub op() End Interface Interface Right Sub op(ByVal i As Integer)
7
by: glen | last post by:
Hi. I'm using GCC 4.1.1, which I mention since I don't know if this is a compiler issue, or me not understanding some subtlety in the standard. The code below compiles fine under vc++, but I'm...
2
by: Tim H | last post by:
Why is this ambiguous: ------------------------------------------------ #include <boost/shared_ptr.hpp> class base {}; class derived: public base {}; class other {}; void...
9
by: neildferguson | last post by:
I am using templates with a little project I am working on. My compiler (GCC) is finding a particular construct ambiguous. Can anyone suggest something I might change in the declaration of class...
3
by: i3x171um | last post by:
To start off, I'm using GCC4. Specifically, the MingW (setjmp/longjmp) build of GCC 4.2.1 on Windows XP x64. I'm writing a class that abstracts a message, which can be either an integer (stored as...
9
by: sebastian | last post by:
I've simplified the situation quite a bit, but essentially I have something like this: struct foo { }; void bar( foo const & lhs, foo const & rhs )
1
by: Ruki | last post by:
I want to overload function to swap two others types, for example, type<T>, T* and so on, but I can't compile the following code at VC 6.0. The compiler says : error C2667: 'swap' : none of 2...
3
by: mathieu | last post by:
Could someone please tell me what is wrong with the following -ugly- piece of c++ code. Why when I explicititely set the template parameter my gcc compiler start getting confused: bla.cxx: In...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.