error C2668: 'sqrt' : ambiguous call to overloaded function

I am new to c++ and I keep getting the c2668 error.

My assignment for the class is to change the program so that it compiles but not removing any statements, just use type casts to get rid of the warnings and errors. I have been spinning my wheels all weekend on this. Any thoughts?

My code is

#include <iostream> // for output functions
#include <cmath> // for math functions, sqrt and pow
using namespace std;

int main ()
{
double dx;
float fx;
int ix;

dx = 2.3e5;
fx = dx;
ix = fx;

cout << sqrt (dx) << endl;
cout << pow (sqrt(dx), 2) << endl;
cout << sqrt (fx) << endl;
cout << pow (sqrt(fx), 2) << endl;
cout << sqrt (ix) << endl;
cout << pow (sqrt(ix), 2) << endl;

return 0;
}
closed account (oG8U7k9E)
There is nothing wrong--I just compiled it--no problem.
really?? im using visual studio 2005 set to warning level 2. i keep getting the c2668 error and the c4244 warning for the possible loss of data when i compile.
closed account (oG8U7k9E)
Your problem is IDE specific--you need someone to show you how to use Visual Studio 2005--I use the gcc compiler so I cannot help.
There are two pow/sqrt functions, one that takes a float and returns a float, and one that takes a double and returns a double. In the cast of your float/int, it doesn't know whether to cast it up to a double or just use the float function. Cast them to doubles and it should fix the issue.
when you say cast them to doubles, do you mean making the following changes?

double dx
double fx
double ix
closed account (oG8U7k9E)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
#include <iostream> // for output functions
#include <cmath> // for math functions, sqrt and pow
using namespace std;

int main (){
	double dx;
	float fx;
	int ix;

	dx = 2.3e5;
	fx = dx;
	ix = fx;

	cout << sqrt (dx) << endl;
	cout << pow (sqrt(dx), 2) << endl;
	cout << sqrt (double(fx)) << endl;
	cout << pow (sqrt(double(fx)), 2) << endl;
	cout << sqrt (double(ix)) << endl;
	cout << pow (sqrt(double(ix)), 2) << endl;

	return 0;
}
that is awesome..thank you so much. that makes so much more sense than what i was doing. you guys should be teaching my course. again thank you.
Topic archived. No new replies allowed.