1

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.

Community
  • 1
  • 1
  • There is no fix you're supposed to use "..." and not whatever else is in your pdf. You could just cast your int to a double: `sqrt(static_cast(x))` – Borgleader Dec 14 '12 at 06:59
  • I'd just make `getSqrRoot` take a `double`. – chris Dec 14 '12 at 06:59
  • 3
    Avoid any *C++ in X period of time* book unless said period of time is "two lifetimes". There is a list of good books here: http://stackoverflow.com/questions/388242/the-definitive-c-book-guide-and-list – R. Martinho Fernandes Dec 14 '12 at 07:00
  • 2
    @perreal, Not even the `using namespace std;` really. – chris Dec 14 '12 at 07:04
  • I'm surprised the supplied code doesn't give more errors, e.g. for the UNICODE quotation marks `“` around the strings. – Some programmer dude Dec 14 '12 at 08:17
  • @perreal No, _don't_ add `using namespace std;`. – Some programmer dude Dec 14 '12 at 08:19
  • Well, a book that promises you to learn C++ in 14 days is likely to propagate the use of `` in C++ code or, even better, mix things like `cin` (which misses a `std::` qualification by the way) with `getch`. You should just be thankful you didn't buy this in printed form. – Christian Rau Dec 14 '12 at 08:44

2 Answers2

3

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!
Arsenii Fomin
  • 3,120
  • 3
  • 22
  • 42
1

Change your getSqrRoot Function to the below

void getSqrRoot(char* buff, float x)
{

And similarly fix the declaration in the first line.

This is happening because std::sqrt which is the function you are using to get the square root, can take either a float or a double but you have given it an int which leads to the confusion since the compiler now has no idea which function to call.

Karthik T
  • 31,456
  • 5
  • 68
  • 87