B
B
baykonurr2017-06-09 15:51:18
C++ / C#
baykonurr, 2017-06-09 15:51:18

Why does it give an error - ambiguous call to an overloaded function?

There is an elementary program.

#include <iostream>
#include <math.h>
#include <iomanip> 
using namespace std;
int main()
{
  int x1, x2, y1, y2;
  double l;
  cin >> x1 >> x2 >> y1 >> y2;
  x1=x2-x1;
  y1=y2-y1;
  l=sqrt((x1*x1)+(y1*y1));
  cout << fixed << setprecision(2) << l;
  return 0;
}

and swear at her like this
c:\users\home\documents\visual studio 2010\projects\zadacha_2\zadacha_2\zadacha_2.cpp(12): error C2668: sqrt: ambiguous call to overloaded function
1> c:\program files ( x86)\microsoft visual studio 10.0\vc\include\math.h(589): could be "long double sqrt(long double)"
1> c:\program files (x86)\microsoft visual studio 10.0\vc\include\ math.h(541): or "float sqrt(float)"
1> c:\program files (x86)\microsoft visual studio 10.0\vc\include\math.h(127): or "double sqrt(double)"
1> when trying to match the argument list "(int)"
I'm a beginner so I'm asking for help to figure out what the error is.

Answer the question

In order to leave comments, you need to log in

2 answer(s)
D
Denis Zagaevsky, 2017-06-09
@baykonurr

sqrt((double)((x1*x1)+(y1*y1)))

A
Anton Zhilin, 2017-06-11
@Anton3

1. Instead of , in C++ you need to use . It's more performant in some cases, and there are more features for C++. 2. Why introduce , if we still calculate ? Then the program will not go crazy with large coordinates. 3. Instead , you can use without loss of performance. Total:#include <math.h>#include <cmath>

#include <iostream>
#include <cmath>
#include <iomanip>
using namespace std;

int main()
{
    double x1, x2, y1, y2;
    cin >> x1 >> x2 >> y1 >> y2;
    cout << fixed << setprecision(2) << sqrt(pow(x2 - x1, 2) + pow(y2 - y1, 2));
    return 0;
}

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question