473,226 Members | 1,305 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,226 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 4609
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: VivesProcSPL | last post by:
Obviously, one of the original purposes of SQL is to make data query processing easy. The language uses many English-like terms and syntax in an effort to make it easy to learn, particularly for...
0
by: jianzs | last post by:
Introduction Cloud-native applications are conventionally identified as those designed and nurtured on cloud infrastructure. Such applications, rooted in cloud technologies, skillfully benefit from...
0
by: abbasky | last post by:
### Vandf component communication method one: data sharing ​ Vandf components can achieve data exchange through data sharing, state sharing, events, and other methods. Vandf's data exchange method...
0
by: fareedcanada | last post by:
Hello I am trying to split number on their count. suppose i have 121314151617 (12cnt) then number should be split like 12,13,14,15,16,17 and if 11314151617 (11cnt) then should be split like...
1
by: davi5007 | last post by:
Hi, Basically, I am trying to automate a field named TraceabilityNo into a web page from an access form. I've got the serial held in the variable strSearchString. How can I get this into the...
0
by: DolphinDB | last post by:
The formulas of 101 quantitative trading alphas used by WorldQuant were presented in the paper 101 Formulaic Alphas. However, some formulas are complex, leading to challenges in calculation. Take...
0
by: DolphinDB | last post by:
Tired of spending countless mintues downsampling your data? Look no further! In this article, you’ll learn how to efficiently downsample 6.48 billion high-frequency records to 61 million...
0
by: Aftab Ahmad | last post by:
Hello Experts! I have written a code in MS Access for a cmd called "WhatsApp Message" to open WhatsApp using that very code but the problem is that it gives a popup message everytime I clicked on...
0
by: Aftab Ahmad | last post by:
So, I have written a code for a cmd called "Send WhatsApp Message" to open and send WhatsApp messaage. The code is given below. Dim IE As Object Set IE =...

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.