Shouldn't the compiler automatically cast to a double in the following? At least according to Walter Savitch.
#include <iostream>
#include <cmath>
using namespace std;
int main(){
int k;
for(k = 1; k <= 10; k++)
cout << "The square root of k is: " << sqrt(k) << endl;
return 0;
}//error C2668: 'sqrt' : ambiguous call to overloaded function
//Visual Studio 2008 on Win Vista
The problem is that there are three versions of sqrt
to choose from:
double sqrt ( double x );
float sqrt ( float x );
long double sqrt ( long double x );
Since you're passing in an int
, the compiler is going to promote your argument but it's equally valid to promote your integer to any of the above types, so it's ambiguous.
You can fix this by simply explicitly casting to one of the above types, as in:
cout << "The square root of k is: " << sqrt((double)k) << endl;
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