0

My code doesn't seem to compile. I receive an error saying:

cannot convert 'Point**' to 'Point*' for argument '1'

This error occurs on both lines of the function call. How may I fix this?

I just recently changed my code from passing by reference to strictly pointers.

// This program computes the distance between two points
// and the slope of the line passing through these two points.

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

struct Point
{
    double x;
    double y;
};

double dist(Point *p1, Point *p2);
double slope(Point *p1, Point *p2);

int main()
{
    Point *p1, *p2;
    char flag = 'y';
    cout << fixed << setprecision(2);
    while(flag == 'y' || flag == 'Y')
    {
        cout << "First x value: "; cin >> p1->x;
        cout << "First y value: "; cin >> p1->y;
        cout << "Second x value: "; cin >> p2->x;
        cout << "Second y value: "; cin >> p2->y; cout << endl;

        cout << "The distance between points (" << p1->x << ", " << p1->y << ") and (";
        cout << p2->x << ", " << p2->y << ") is " << dist(&p1, &p2);

        if ((p2->x - p1->x) == 0)
        { cout << " but there is no slope." << endl; cout << "(Line is vertical)" << endl; }
        else
        { cout << " and the slope is " << slope(&p1, &p2) << "." << endl; }

        cout << endl;
        cout << "Do you want to continue with another set of points?: "; cin>> flag;
        cout << endl;
    }
    return 0;
}

double dist(Point *p1, Point *p2)
{
    return sqrt((pow((p2->x - p1->x), 2) + pow((p2->y - p1->y), 2)));
}

double slope(Point *p1, Point *p2)
{
    return (p2->y - p1->y) / (p2->x - p1->x);
}
JasonMArcher
  • 14,195
  • 22
  • 56
  • 52
Cody
  • 37
  • 6
  • 1
    What do you mean by "incorporate pointers into the program?" It doesn't look like you need pointers here. – templatetypedef Sep 10 '14 at 02:49
  • True. My program compiles perfectly but for the sake of satisfying the project requirements, I need to convert it to pointers. Rather than passing by reference with pointers, I need to strictly use pointers. @templatetypedef – Cody Sep 10 '14 at 02:52
  • 1
    At least you could have uploaded the corrected version of the code. – Javi Sep 10 '14 at 02:55
  • Your dist and slope functions can take const pointers or references. – Neil Kirk Sep 10 '14 at 02:57
  • Also see [Pointer to pointer clarification](http://stackoverflow.com/questions/21604946/pointer-to-pointer-clarification). – jww Sep 10 '14 at 21:29

2 Answers2

1

To be perfectly honest, your code is fine without pointers. There's no real need to introduce them here.

If you must introduce pointers as part of an assignment and use pass-by-pointer rather than pass-by-reference, then you need to make a few changes.

  1. Change the signatures of the functions that take in Points by reference to take them in by pointer instead. For example, you'd have `double slope(Point* p1, Point* p2).

  2. At the call sites to these functions, explicitly pass in the address of the parameter. For example, instead of calling slope(p1, p2), write slope(&p1, &p2).

  3. Update the function bodies whose arguments are Point*s to use -> instead of . to access their fields.

Hope this helps!

templatetypedef
  • 362,284
  • 104
  • 897
  • 1,065
0

All you have to do is to receive the elements as pointers, send them as references and use the operator ->:

double mydistance(Point *p1, Point *p2)
{
    return sqrt((pow((p2->x - p1->x), 2) + pow((p2->y - p1->y), 2)));
}

And calling with

mydistance(&p1,&p2);

Actually, as pointed out in your previous post, rename the distance function or remove the using namespace std since you are calling std::distance(). That is why I called here mydistance().

Javi
  • 3,440
  • 5
  • 29
  • 43