can anyone tell me why my program wont compile?

I keep getting this error
1
2
3
4
5
6
7
8
9
10
11
12
13
14
calculate.cpp: In function `int main()':
calculate.cpp:33: call of overloaded `sqrt(int&)' is ambiguous
/usr/local/lib/gcc-lib/sparc-sun-solaris2.7/3.2.2/include/math.h:154: candidates
   are: double sqrt(double)
/usr/local/include/c++/3.2.2/cmath:465:                 long double 
   std::sqrt(long double)
/usr/local/include/c++/3.2.2/cmath:461:                 float std::sqrt(float)
calculate.cpp:34: call of overloaded `sqrt(int&)' is ambiguous
/usr/local/lib/gcc-lib/sparc-sun-solaris2.7/3.2.2/include/math.h:154: candidates
   are: double sqrt(double)
/usr/local/include/c++/3.2.2/cmath:465:                 long double 
   std::sqrt(long double)
/usr/local/include/c++/3.2.2/cmath:461:                 float std::sqrt(float)
  

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
#include <iostream>
#include <cmath>
using namespace std;
int main()
{
cout << "(c) 2012, bfields Byron Fields." << endl;

int num1;
int num2;
int sum;
int product;
int quotient;
int remainder;
float average;
int square;
int square2;
double squareroot;
double squareroot2;

cout << "Please enter the first integer: " << endl;
cin >> num1;

cout << "Please enter the second integer: " << endl;
cin >> num2;

sum = num1 + num2;
product = num1*num2;
quotient = num1/num2;
remainder = num1%num2;
average = sum/2;
square = num1*num1;
square2 = num2*num2;
squareroot = sqrt (num1);
squareroot2 = sqrt (num2);

cout << "The sum of the two integers is : " << sum << endl;

cout << "The product of the two integers is : " << product << endl;

cout << "The quotient of the two integers is : " << quotient << endl;

cout << "The remainder of the two integers is : " << remainder << endl;

cout << "The average of the two integers is : " << average << endl;

cout << "The square of the first integer is : " << square << endl;

cout << "The square of the second integer is : " << square2 << endl;

cout << "The square root of the first integer is : " << squareroot << endl;

cout << "The square root of the second integer is : " << squareroot2 << endl;

return 0;
}
There are several functions sqrt that have parameters of different types. But among these functions no one has the parameter declared as having type int. So the compiler does not know which existent function to prefer for argument of type int.
I suggest to cast the argument to double as, for example

sqrt ( ( double )num1);
Last edited on
Try casting to one of those three types like the compiler suggests.
squareroot = sqrt ( static_cast<double>(num1));
C++ does allow for implicit type casting between double and int, float and int, and long double and int. However, the compiler doesn't know what into cast the int to in this situation. It can pick any of three different things which is ambiguous to the compiler.
See typecasting here for other ways to do this:
http://cplusplus.com/doc/tutorial/typecasting/
Last edited on
Topic archived. No new replies allowed.