简体   繁体   中英

Compilation errors when calculating distance between two points

I can't figure out why this program won't work. I'm sure it's something basic.

#include <iostream>
using namespace std;
class MyPoint
{
    int x;
    int y;
public:
    MyPoint()
    {
        x = 0;
        y = 0;
    }

    MyPoint(int newX, int newY)
    {
        x = newX;
        y = newY;
    }

    int getX()
    { 
        return x;
    }

    int getY()
    {
        return y;
    }

    int distance(MyPoint newPoint)
    {
      distance = x - newPoint.getX();//need absolute value function
      return distance;
    };

  int main()
  {
    MyPoint point1(0,0);
    MyPoint point2(5,5);

    cout << "THe distance between the two circles is " << point1.distance(point2) << endl;

    return 0;
  }

I'm trying to find the distance between two points and just to test to make sure that I am using classes correctly. I am just using the x point only. Right now the code will not compile.

Now, your first problem here is that main is inside the class - you forgot a close brace after the distance function. You've got the semicolon for ending the class description, but you have in total one close-brace too few.

Your second problem is that you're using a variable named distance inside a function named distance . Don't make name collisions, they make kittens cry.

Your third problem is that the distance variable that I just mentioned should be of type int .

As another piece of general advice, compilers give you error messages when your code doesn't compile. Posting those is helpful.

Your problem lies in the distance function:

int distance(MyPoint newPoint)
{
  distance = x - newPoint.getX();//need absolute value function
  return distance;
};

Here, you have a variable named distance , which is not declared anywhere, and also is overloaded with the function name. Instead, you need to declare a new local variable and then return it, or just return it right away:

Option 1:

int distance(MyPoint newPoint)
{
  int d = x - newPoint.getX(); // renamed d, adding int
  return d;
};

Option 2:

int distance(MyPoint newPoint)
{
  return x - newPoint.getX();
};

1) Close brace after distance function.
2) Implementation of distance function is not correct.

If you really want it to calc distance between two points, you should write something like:

double distance(MyPoint newPoint)
{
  double distance;
  distance=sqrt((x-newPoint.getX())*(x-newPoint.getX())
                    +(y-newPoint.getY())*(y-newPoint.getY()));
  return distance;
}

It'll find distance using Pythagorean theorem.
1) declaration of variable distance with type double.
2) calculating distance using Pythagorean theorem.
3) returning value of variable.

You have a mistake in the braces.

The code below runs correctly: (EDIT) - added an abs as correctly pointed out by AIs

#include <iostream>
using namespace std;
class MyPoint
{
int x;
int y;
public:
MyPoint()
{
    x = 0;
    y = 0;
}

MyPoint(int newX, int newY)
{
    x = newX;
    y = newY;
}

int getX()
{
    return x;
}

int getY()
{
    return y;
}

int distance(MyPoint newPoint)
{
  return abs(newPoint.getX() - x);//need absolute value function

}
};

int main()
{
MyPoint point1(0,0);
MyPoint point2(5,5);

cout << "THe distance between the two circles is " << point1.distance(point2) << endl;

return 0;
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM