模糊的超载 [英] ambiguous overload

查看:55
本文介绍了模糊的超载的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

#include< iostream>

使用命名空间std;

class Integer {

int i;

public:

整数(int ii):i(ii){}

const整数运算符+(const整数& rv){

cout< <" 1-operator +"<< endl;

返回Integer(i + rv.i);

}

整数运算符+ =(整数rv){

i + = rv.i;

cout<<"" 2-operator + ="<< endl;

返回* this;

}

朋友整数& operator +(整数,整数);


void display(){

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

}

};

整数& operator +(整数rv,整数rs){

cout<<" 2-operator + ="<< endl;

rv.i + = rs.i ;

返回rv;

}

int main(无效){

cout<<" buit -in types"<< endl;

int i = 1,j = 2,k = 3;

k + = i + j;

cout<<<"用户定义类型:"<< endl;

整数ii(1),jj(2),kk(3);

kk + = ii + jj;

整数d(1),b(2);

d = 2 + b;

d .display();

返回0;

}


g ++ -g -Wall -o测​​试测试.cpp

test.cpp:在函数''整数& operator +(Integer,Integer)'':

test.cpp:22:警告:返回对本地变量''rv''的引用

test.cpp:在函数中'int main()'':

test.cpp:33:错误:''运算符+''在''ii + jj'中的模糊重载''

test。 cpp:7:注意:候选者是:const Integer Integer :: operator +

(const Integer&)

test.cpp:22:注意:整数& operator +(整数,

整数)

make:*** [test]错误1

??


我该怎么做才能防止这种情况?

#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 ??

推荐答案

onkar写道:
onkar wrote:

#include< iostream>

使用命名空间std;

class Integer {

int i;

public:

整数(int ii):i(ii){}

const整数运算符+(const整数& rv){

cout< <" 1-operator +"<< endl;

返回Integer(i + rv.i);

}

整数运算符+ =(整数rv){

i + = rv.i;

cout<<"" 2-operator + ="<< endl;

返回* this;

}

朋友Inte蒙古包和放大器; operator +(整数,整数);


void display(){

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

}

};

整数& operator +(整数rv,整数rs){

cout<<" 2-operator + ="<< endl;

rv.i + = rs.i ;

返回rv;
#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 ??



坚持一个运营商+。为什么有两个?


-

Ian Collins。

Stick to one operator+. Why have two?

--
Ian Collins.


1月7日,11: 02:00,Ian Collins< ian-n ... @ hotmail.comwrote:
On Jan 7, 11:02 am, Ian Collins <ian-n...@hotmail.comwrote:

onkar写道:
onkar wrote:

#include< iostream>

使用命名空间std;

class Integer {

int i;

public:

整数(int ii):i(ii){}

const整数运算符+(const整数& rv){

cout< <" 1-operator +"<< endl;

返回Integer(i + rv.i);

}

整数运算符+ =(整数rv){

i + = rv.i;

cout<<"" 2-operator + ="<< endl;

返回* this;

}

朋友Integer&操作者+(整数,整数);
#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;

}

};

整数& operator +(整数rv,整数rs){

cout<<" 2-operator + ="<< endl;

rv.i + = rs.i ;

返回rv;
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 ??



坚持一个运营商+。为什么有两个?


-

Ian Collins。


Stick to one operator+. Why have two?

--
Ian Collins.



#include< iostream>

使用命名空间std;

class Integer {

int i;

public:

整数(int ii):i(ii){}

/ * const整数运算符+(const整数& rv){

cout<<"" 1-operator +"<< endl;

返回Integer(i + rv.i);

} * /

整数运算符+ =(整数rv){

i + = rv.i;

cout< <" 2-operator + ="<< endl;

return * this;

}

朋友Integer& operator +(整数,整数);


void display(){

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

}

};

整数& operator +(整数rv,整数rs){

cout<<" 4-operator +"<< endl;

rv.i + = rs.i ;

返回rv;

}

int main(无效){

cout<<" buit -in types"<< endl;

int i = 1,j = 2,k = 3;

k + = i + j;

cout<<<"用户定义类型:"<< endl;

整数ii(1),jj(2),kk(3);

kk + = ii + jj;

整数d(1),b(2);

d = 2 + b;

d .display();

返回0;

}


如何防止此警告?

g ++ -g -Wall -o test test.cpp

test.cpp:在函数''整数& operator +(Integer,Integer)'':

test.cpp:22:警告:返回对本地变量''rv''的引用


注意:如果我删除&在

整数& operator +(整数rv,整数rs)


g ++ -g -Wall -o test test.cpp

test.cpp:在函数''整数运算符+(整数,整数)'':

test.cpp:22:错误:新声明''整数运算符+(整数,

整数)''

test.cpp:16:错误:模糊旧声明''整数& operator +

(整数,整数)''

make:*** [test]错误1


??僵局的情况?? :(

#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 ?? :(


onkar写道:
onkar wrote:

1月7日上午11点02分,Ian Collins< ian -n ... @ hotmail.comwrote:
On Jan 7, 11:02 am, Ian Collins <ian-n...@hotmail.comwrote:

> onkar写道:
>onkar wrote:


>>整数&运算符+(整数rv,整数rs){
cout<<" 2-operator + = "<< endl;
rv.i + = rs.i;
返回rv;
>>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 ??



我是你上次老了。


-

Ian Collins。

I told you last time.

--
Ian Collins.


这篇关于模糊的超载的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆