Possible Duplicate:
C++ compiler error: ambiguous call to overloaded function
just copied some code from a pdf into both C++builder XE2 and visual studio express 2012. both both compilers give error codes about ambiquity. I just started so i don't really know what to do. Maybe my textbook(pdf) is old and obsolete now? it's called "Learn c++ in 14 days". Well anyways here is the copied code.
#include <iostream.h>
#include <conio.h>
#include <math.h>
#include <stdio.h>
#pragma hdrstop
void getSqrRoot(char* buff, int x);
int main(int argc, char** argv)
{
int x;
char buff[30];
cout << “Enter a number: “;
cin >> x;
getSqrRoot(buff, x);
cout << buff;
getch();
}
void getSqrRoot(char* buff, int x)
{
sprintf(buff, “The sqaure root is: %f”, sqrt(x));
}
the errorcode i got in c++builder is:
[BCC32 Error] SquareRoot.cpp(19): E2015 Ambiguity between 'std::sqrt(float) at c:\program files (x86)\embarcadero\rad studio\9.0\include\windows\crtl\math.h:266' and 'std::sqrt(long double) at c:\program files (x86)\embarcadero\rad studio\9.0\include\windows\crtl\math.h:302' Full parser context SquareRoot.cpp(18): parsing: void getSqrRoot(char *,int)
On a side note, the quotation marks in my pdf manual are different characters than the normal " which i type. these “ are also not compatible with the compiler. maybe anybody knows a fix for this as well? thanks in advance.
Change your code like that:
void getSqrRoot(char* buff, int x)
{
sprintf(buff, “The sqaure root is: %f”, sqrt((float)x));
}
Because square root is overloaded function compiler has no opportunity to implicit conversion from int x value to float or double value, you need to do it directly.
Compiler: see sqrt(int) -> what to choose? sqrt(float)/sqrt(double) ?
Compiler: see sqrt((float)int) -> sqrt(float), ok!
Compeler: see sqrt((double)int) -> sqrt(double), ok!