Overloading Function

Aug 21, 2014 at 8:26pm
Here I have this class and am trying to calculate a distance and I keep getting an overload function error. I have tried type casting, and have tried solving the problem a different way but can't seem to get rid of the error. Any help is greatly appreciated!

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
33
34

class City
{
private:
    int x;
    int y;

public:
    //constructs a city at chosen x,y location
    City(int x, int y)
    {
        this->x = x;
        this->y = y;
    }

    int getX()
    {
        return this->x;
    }

    int getY()
    {
        return this->y;
    }

    double distanceTo(City city)
    {
        int xDistance = abs(getX() - city.getX());
        int yDistance = abs(getY() - city.getY());
        double distance = sqrt((xDistance*xDistance)+(yDistance*yDistance));

        return distance;
    }
};
Last edited on Aug 21, 2014 at 8:27pm
Aug 21, 2014 at 9:06pm
> I keep getting an overload function error
If you don't understand the error message then don't try to paraphrase it, just post it verbatim.
Aug 21, 2014 at 9:10pm
Which operator do you intend overloading?

Aceix.
Aug 22, 2014 at 1:32am
I'm getting:

'sqrt' ambiguous call to overloaded function

And then warning - 'return' : conversion from double to int, possible loss of data
Aug 22, 2014 at 1:39am
If you're including <cmath>, try making it std::sqrt(n) instead of just sqrt().
That might fix the other problem too, I'm not sure what would be causing the name conflict for you though.
Aug 22, 2014 at 3:38am
Hmm.. that didn't work. Any other ideas? I tried separating out each part so abs and sqrt only handle one variable but that didn't work either.
Aug 22, 2014 at 3:41am
Whenever you get an "ambiguous call" type error, you need to explicitly state the type of the parameter.
1
2
int x = 648;
double y = sqrt((double)x)
Topic archived. No new replies allowed.