C++

Error: Call of Overloaded is Ambiguous

In this article, we will discuss an error that occurs in C++ Language that is Call of Overloaded is Ambiguous. This error does not affect how the code is run because it occurs at compile time. We need to know what overloading is. In overloading, we declare functions with the same names but different datatypes. The system differentiates between the functions because of their datatypes. Ambiguous refers to having multiple meanings or similarities. In other ways, we can say that ambiguous means unclear. So, the error explains itself.

The error occurs when a function overloading the compiler interprets the functions as a single function and cannot make a difference between the two overloaded functions based on their datatypes. So, we can say that this error occurs because of the inability of the compiler to make a selection between two rightly overloaded methods. This whole process is said to be ambiguous because it is unclear. Codes and programs containing such ambiguities cannot compile and eventually generate errors.

Syntax

This is not a built-in function or library we are discussing, rather it is an error, and an error does not have a specific syntax. This is the message that the compiler returns when we get our error.

error: call of overloaded ‘setval(int)’ is ambiguous

Note: The word ‘setval(int)’ is not constant.

Example # 01:

To have a better understanding of our error we will now perform an example to get the error and after that, we will resolve the error. To do so, we have declared a class with the name class1. In class1, we have declared private unsigned char data[BYTES]. In our class’s public section, we have declared two functions with the same name, “samefunc”. But we have passed “unsigned int” to one as a parameter and “const char” to the other as a parameter so that we can overload the methods by having the same names and different datatypes as parameters. In the first “samefunc”, we have printed “int” with the value passed to it. In the second function, we have printed “char” with the value passed to our second function. With the use of an object within our class, we called the method the main method. Now, we will execute our code to see what happens.

#include <iostream>

#define BYTES 8
using namespace std ;
class class1
{
private:
    unsigned char data[BYTES];
public:
void samefunc(int);
void samefunc(const char *);  
};
void class1::samefunc(int t)
{
cout<<"int"<<t<<endl;
}
void class1::samefunc(const char *s)
{
cout<<"char"<<s<<endl;
}
int main() {
    class1 p;
p.samefunc(0);
    return 0;
}

After the execution of our code, the system has given us an error message. If we take a look at the error message, we can see that the message is almost the same as we discussed earlier. The only difference is the function name. We told you earlier that the function name will keep on changing depending on what you have declared as the name of your function. Anyways, the system has given us the error that “call of overload is ambiguous”. A question arises that we have passed “0” to our function and “0” is an integer then why has the system given us this error? Let’s dig out the reason and try to resolve the error.

Now, to resolve the error we will not retype our whole code again we have simply removed the keyword “unsigned” before “int” in our first function. After that, we executed our code and the code has been executed successfully.

The error was caused because the value “0” has two meanings in the C++ language. Firstly, it is an integer. Secondly, in the case of characters, it is a null pointer constant. The compiler was unable to decide in our case whether our input parameter was an integer or a null pointer constant. We were using two datatypes for our methods int and char. So, the compiler got confused. After removing the keyword, the compiler took the parameter as an integer and executed the function with an integer datatype.

Example # 02:

To make you understand better the error being discussed. We have performed another example here. In this example, we have declared two methods with the same names “addvals”. But the difference between the two functions is that one is taking integers as input parameters and the other is taking floats as input parameters. Both methods have the same purpose, they are taking input parameters and sum their values up. In the main method, we printed the result after they have performed operations on our input parameters. We will execute our code to check the output.

#include<iostream>

using namespace std;
int addvals(int x,int y)
{
   return x+y;
}
int addvals(float x, float y){
   return x+y;
}
int main(){
cout<<addvals(1.2, 3.4);
   return 0;
}

Once again, the system has not given us any output. Instead, it has thrown an error message. The error message is again the same as the previous one. But the function name is different. In this case, we passed values “1.2” and “3.4” to our function which is clear that these values are floats. A question may arise on why the system has not performed its job and given us the error. To get the reason, we will try to resolve the error and by doing so we will get our answer as well.

Now, to resolve the error we have put “1.2f” and “3.4f” with our parameters. We will execute the code again to see whether this approach helps in the resolution of our error or not. Let’s again execute our code to see what happens.

The system has not given us any error this time and has successfully executed our code. So, the problem in our code was that we were not putting “f” in our parameters. We assumed that the compiler would read them as float values instead of integers. But we were wrong because the compiler identifies all the floats as double initially unless we put “f” as a suffix. Once we put our suffix the code has been compiled successfully.

Conclusion

In this guide, we discussed another error which is “Call of overload (function) is ambiguous”. We discussed why the error occurs. We also discussed the concept of function overloading to understand our error in a better way. Examples were performed to help you get a better grip on resolving the error.

About the author

Omar Farooq

Hello Readers, I am Omar and I have been writing technical articles from last decade. You can check out my writing pieces.