[英]C++,function overloading problem
I am having a trouble in this example.Whenever I send any arguments,it gives compiler error: 我在此示例中遇到了麻烦。每当我发送任何参数时,都会出现编译器错误:
prog.cpp: In function ‘int main()’:
prog.cpp:11: error: call of overloaded ‘add(std::basic_istream<char, std::char_traits<char> >&, std::basic_istream<char, std::char_traits<char> >&)’ is ambiguous
prog.cpp:4: note: candidates are: int add(int, int) <near match>
prog.cpp:6: note: char add(char, char) <near match>
I personally think that this error should occur when I am sending the arguments as int's or char's but when I send float arguments,the error still remains.Please help.Thanks 我个人认为当我将参数发送为int或char时应该发生此错误,但是当我发送float参数时,该错误仍然存在。请帮助。
/*Function Overloading*/
#include<iostream>
using namespace std;
int add(int a,int b);
float add(float a,float b);
char add(char a,char b);
int main()
{
float a,b;
cout<<"Enter Two numbers to add them"<<'\n';
add(cin>>a,cin>>b);
return 0;
}
int add(int a,int b)
{
//cin>>a,b;
return a+b;
}
float add(float a,float b)
{
//cin>>a,b;
return a+b;
}
char add(char a,char b)
{
//cin>>a,b;
return a+b;
}
cin >> x
returns cin
, not x
. cin >> x
返回cin
,而不是x
。 Try 尝试
cin >> a >> b;
add(a, b);
Also syntax like cin >> a, b
is invalid, use >> multiple times. 同样,
cin >> a, b
类的语法无效,请多次使用>>。
Instead of 代替
add(cin>>a,cin>>b);
write 写
cin>>a;cin>>b;
add(a,b);
The stream read operation returns the stream, not the value read. 流读取操作返回流,而不是读取的值。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.