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?
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.
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.
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.
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());
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With