Overloading Function

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
> 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.
Which operator do you intend overloading?

Aceix.
I'm getting:

'sqrt' ambiguous call to overloaded function

And then warning - 'return' : conversion from double to int, possible loss of data
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.
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.
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.