Although polymorphism is a widely useful phenomena in C++ yet it can be quite complicated at times. For instance consider the following code snippet:
#include<iostream> using namespace std; void test(float s,float t) { cout << "Function with float called "; } void test(int s, int t) { cout << "Function with int called "; } int main() { test(3.5, 5.6); return 0; } |
It may appear that the call to the function test in main() will result in output “Function with float called” but the code gives following error:
In function 'int main()': 13:13: error: call of overloaded 'test(double, double)' is ambiguous test(3.5,5.6);
It’s a well known fact in Function Overloading, that the compiler decides which function needs to be invoked among the overloaded functions. If the compiler can not choose a function amongst two or more overloaded functions, the situation is -” Ambiguity in Function Overloading”.
- The reason behind the ambiguity in above code is that the floating literals 3.5 and 5.6 are actually treated as double by the compiler. As per C++ standard, floating point literals (compile time constants) are treated as double unless explicitly specified by a suffix [See 2.14.4 of C+++ standard here). Since compiler could not find a function with double argument and got confused if the value should be converted from double to int or float.
Rectifying the error: We can simply tell the compiler that the literal is a float and NOT double by providing suffix f.
Look at the following code :
#include<iostream> using namespace std; void test(float s,float t) { cout << "Function with float called "; } void test(int s,int t) { cout << "Function with int called "; } int main() { test(3.5f, 5.6f); // Added suffix "f" to both values to // tell compiler, it's a float value return 0; } |
Output:
Function with float called
This article is contributed by Aakash Sachdeva. If you like GeeksforGeeks and would like to contribute, you can also write an article and mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.
Please write comments if you find anything incorrect, or you want to share more information about the topic discussed above
Attention reader! Don’t stop learning now. Get hold of all the important DSA concepts with the DSA Self Paced Course at a student-friendly price and become industry ready.
Recommended Posts:
- Function Overloading in C++
- Does C support function overloading?
- Function overloading and return type
- Function overloading and const keyword
- C++ | Function Overloading and Default Arguments | Question 5
- C++ | Function Overloading and Default Arguments | Question 2
- C++ | Function Overloading and Default Arguments | Question 3
- C++ | Function Overloading and Default Arguments | Question 4
- C++ | Function Overloading and Default Arguments | Question 5
- Function Overloading vs Function Overriding in C++
- Operator Overloading in C++
- Constructor Overloading in C++
- Rules for operator overloading
- Overloading New and Delete operator in c++
- C++ | Operator Overloading | Question 10
- C++ | Operator Overloading | Question 9
- C++ | Operator Overloading | Question 10
- C++ | Operator Overloading | Question 7
- C++ | Operator Overloading | Question 6
- C++ | Operator Overloading | Question 5

