Help needed

My program wont run it keeps giving me " error C2668: 'cos' : ambiguous call to overloaded function" & " error C2668: 'sin' : ambiguous call to overloaded function " but i have no idea how to fix it.

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
#include <iostream>
#include <math.h>
using namespace std;

int main ()
{
double pi = acos(-1.0);

double x[5] = {0, pi/6, pi/4, pi/3, pi/2};

double y[5] = {0, pi/6, pi/4, pi/3, pi/2};

double f[5][5];

cout << "x\tb\tc\n\n";

for (int i = 0; i < 5; i++)
{
	for (int j = 0; j < 5; j++)
	{
	 f[i][j] = 2 * sin(i) - cos(j);
		
		cout << x[i] << "\t"<< y[i] << "\t" << f[i][j] << "\n";

	}
cout << "\n";
}

system("pause");

return 0;
}
Line 21: You're passing ints to sin() and cos(). In C++98, sin and cos have overloaded versions accepting doubles, floats and long doubles. The compiler does not know which to use to promote the integer value.

Use a cast to tell the compiler which one you want:
 
f[i][j] = 2.0 * sin((double)i) - cos((double)j);


BTW, <math.h> is not the correct header for a C++ program.
You should be using <cmath>.
Thanks for the help.
Oh and <math.h> = <cmath> according to my professor.
Btw, it is not good practice to use the system() functions. If this is just for fun coding or for school it's fine, but otherwise don't use it.
Last edited on
Oh and <math.h> = <cmath> according to my professor.

Your professor is ignorant. They are not necessarily the same thing, and you should use <cmath> for C++ programs.

Topic archived. No new replies allowed.