I am using function overloading. But why am I getting 'ambiguous call error' when calling the 1st function? How is it ambiguous? | Sololearn: Learn to code for FREE!
New course! Every coder should learn Generative AI!
Try a free lesson
+ 1

I am using function overloading. But why am I getting 'ambiguous call error' when calling the 1st function? How is it ambiguous?

https://code.sololearn.com/cv9KHb5vuUwG/?ref=app

12th Oct 2020, 1:33 PM
Prasenjit Kumar
Prasenjit Kumar - avatar
6 Answers
+ 2
First one can't decide which function should be called ,the one with two float values or the one with two int values on 3rd and 4th place as default and 1st and 2nd parameter being int for both
12th Oct 2020, 1:40 PM
Abhay
Abhay - avatar
+ 1
sum(10,15,0,0); will call 1st function.
12th Oct 2020, 1:59 PM
Jayakrishna 🇮🇳
0
sum(10, 15) ; This will match to both overlaoeded functions.. Which one do you want to call here..?
12th Oct 2020, 1:46 PM
Jayakrishna 🇮🇳
0
Jayakrishna🇮🇳 Say I want to call 1st function. Is there any way to call either of the two functions succesfully and not get error, keeping both the funtion declarations as it is?
12th Oct 2020, 1:51 PM
Prasenjit Kumar
Prasenjit Kumar - avatar
0
Prasenjit Kumar Yes using a C++20 feature - concepts! template<typename T> T sum(T x, T y,T w=0,T z=0) requires is_same_v<int,T> { return w+x+y+z; } template<typename T> T sum(T x, T y,T w=0,T z=0) requires is_same_v<float,T> { return w+x+y+z; }
12th Oct 2020, 3:46 PM
Anthony Maina
Anthony Maina - avatar
0
The compiler is ignoring the default values and takes the declared variable as parameter where the two functions are same
13th Oct 2020, 3:54 AM
Aditya rout
Aditya rout - avatar