1

I want to find the distance between two points. In class point, the parameterized constructor, point(int a, int b), takes two integers and assigns them to the private variables, x and y. The displaypoint() member function prints "(x,y)". It also has friend class, dist.

The class dist has only a constructor that takes two point class objects as arguments and should display a calculated diff variable.

However, my dist class gives an error.

Here's the code.

// Parameterized Constructor using Friend Class Example :-
#include <iostream>
#include <cmath>
using namespace std;

class point
{
    int x, y;
    friend class dist;

public:
    point(int a, int b)
    {
        x = a;
        y = b;
    }
    void displayPoint()
    {
        cout << "The Point is : (" << x << "," << y << ")" << endl;
    }
};
class dist  // shows error here
{
public:
    void dist(point p1, point p2)
    {
        int x_diff = (p2.x - p1.x);
        int y_diff = (p2.y - p1.y);
        int diff = sqrt(pow(x_diff, 2) + pow(y_diff, 2));
        cout << "The difference is : " << diff << endl;
    }
};
int main()
{
    point p(1, 2);
    point q(4, 6);

    point c(1, 1);
    point d(1, 1);

    point e(1, 0);
    point f(70, 0);

    dist(p, q);
    dist(c, d);
    dist(e, f);
    return 0;
}

If I remove class dist and make dist() as a friend function instead of a constructor, the code works.

void dist(point p1, point p2)
{
    int x_diff = (p2.x - p1.x);
    int y_diff = (p2.y - p1.y);
    int diff = sqrt(pow(x_diff, 2) + pow(y_diff, 2));
    cout << "The difference is : " << diff << endl;
}
Adrian Mole
  • 49,934
  • 160
  • 51
  • 83
Aslam Sha
  • 13
  • 3
  • 1
    What's the error? – Alan Birtles Mar 20 '23 at 14:15
  • 1
    constructors don't return any value and don't have return value in their signature, so remove the `void` from `void dist(point p1, point p2)` and it would work just fine – The Wrecker Mar 20 '23 at 14:15
  • Also, don't use `pow` for integers: `x_diff * x_diff` may be a *few* extra keystrokes but it's *much* safer and far more robust. Although you're better off using a `double` type for those differences. – Adrian Mole Mar 20 '23 at 14:18
  • Removing void from the dist() constructor worked! – Aslam Sha Mar 20 '23 at 14:18
  • @AslamSha If you want further proof of `pow` not being safe for integers, [see this question](https://stackoverflow.com/questions/25678481/why-does-pown-2-return-24-when-n-5-with-my-compiler-and-os). – PaulMcKenzie Mar 20 '23 at 14:21
  • Why is `dist` a class? Why does it print the result? In standard C++, is `sqrt` a class? Does it print the result? What about `sin` or `toupper` or `strtol`? – n. m. could be an AI Mar 20 '23 at 14:28
  • @n.m. I made `dist` as class instead of a function to get a better understanding in OOP. – Aslam Sha Mar 20 '23 at 14:50

1 Answers1

1

As mentioned in the comments, your primary issue is that you have declared the dist constructor with a return type (even though that type is void, the declaration is still a syntax error). So, just remove that return type.

Also, you shouldn't be using the pow function with integer types; in fact, you shouldn't be using int as the x_diff and y_diff types, nor for the resultant diff; an integer result for that answer will, most often, be quite wrong (especially if either the x or y differences are small). Use double for all those values:

class dist  // shows error here
{
public:
    dist(point p1, point p2) // Constructor doesn't have a return type
    {
        double x_diff = static_cast<double>(p2.x - p1.x);
        double y_diff = static_cast<double>(p2.y - p1.y);
        double diff = sqrt(pow(x_diff, 2) + pow(y_diff, 2));
        cout << "The difference is : " << diff << endl;
    }
};
Adrian Mole
  • 49,934
  • 160
  • 51
  • 83