C++,函数重载问题
我在这个例子中遇到了麻烦。每当我发送任何参数时,它都会给出编译器错误:
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>
我个人认为当我将参数作为 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;
}
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
/*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;
}
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
cin>> x
返回cin
,而不是x
。 还可以尝试像
cin >> 这样的语法a,b
无效,使用>>多次。cin >> x
returnscin
, notx
. TryAlso syntax like
cin >> a, b
is invalid, use >> multiple times.而不是
write
流读取操作返回流,而不是读取的值。
Instead of
write
The stream read operation returns the stream, not the value read.