Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

C++ compiler error: ambiguous call to overloaded function

Tags:

string aux;
int maxy,auxx=0;

cin>>aux;

maxy= (int)sqrt(aux.size());

I'm geting:

1> error C2668: 'sqrt' : ambiguous call to overloaded function
1>        could be 'long double sqrt(long double)'
1>        or       'float sqrt(float)'
1>        or       'double sqrt(double)'

Why?

like image 952
andandandand Avatar asked Jun 03 '11 22:06

andandandand


People also ask

Can a call to an overloaded function be ambiguous options the?

Answer: If two or more functions have the same name and function signature, the call to an overloaded function can be unclear. Explanation: Function overloading ambiguity occurs when the compiler is unable to decide which of the overloaded functions should be invoked first.

What is ambiguity in function overloading?

When the compiler is unable to decide which function it should invoke first among the overloaded functions, this situation is known as function overloading ambiguity. The compiler does not run the program if it shows ambiguity error.

Why will an ambiguity error arise if a default value is given to an argument of an overloaded function?

Function Overloading and float in C++ 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.


1 Answers

string::size() returns size_t, and sqrt doesn't accept it in any of its versions. So the compiler has to cast, and cannot choose to what - all of them are OK. You have to put explicit cast:

maxy = (int)sqrt((double)aux.size());
like image 140
littleadv Avatar answered Sep 21 '22 15:09

littleadv