1

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;
}

2 Answers 2

6

cin >> x returns cin, not x. Try

cin >> a >> b;
add(a, b);

Also syntax like cin >> a, b is invalid, use >> multiple times.

Sign up to request clarification or add additional context in comments.

2 Comments

cin >> a, b is perfectly valid syntax even if it doesn't do what the askers needs in this situation.
@Charles: Valid in language terms not according to what is needed.
5

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.

1 Comment

I have jumped from C to C++ so I thought that when the compiler will read the function call it will evaluate out the arguments and to do so it will call cin twice which will give me the values I needed.Was I wrong?

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.