Reputation: 17842
i am trying to compile my program where i am using the functions like sqrt pow and fabs. I do have math.h included but for some reason i get errors like:
error C2668: 'fabs' : ambiguous call to overloaded function
same for the rest of the functions i have this included:
#include "stdafx.h"
#include "math.h"
i tried including but still same errors. Does anyone know why they are not being recognized? my file is .cpp not .c but it is an MFC project.
thanks
Upvotes: 2
Views: 4972
Reputation: 17842
It's because those functions are overloaded for several types: float, double and long double. Thus, if you pass in an integer, the compiler doesn't which one to choose. An easy fix is to simply pass a double (or multiply what you pass in by 1.0), this should fix the problem.
int value = rand();
int result1 = (int)fabs(value*1.0);
printf("%d, %d\n", result1, result1);
otherwise:
int value = rand();
int result1 = (int)fabs((double)value);
printf("%d, %d\n", result1, result1);
Upvotes: 8