Error when implementing a TAD Point in C ++

0

The exercise consists in implementing a TAD Point, with the operations abscissa (returns the x), ordered (returns the y) and distance (calculates the distance between two points).

The exercise is simple and this is my code:

file: dot.h

#ifndef PUNTO_H
#define PUNTO_H

class Punto
{
public:

    //Constructores primero
    Punto();
    //Construye un punto a (0,0)
    Punto(float x, float y);
    //Construye un punto con lo que le den

    //Operaciones

    float abscisa();
    //POST: devuelve el valor x del punto actual
    float ordenada();
    //POST devuelve el valor y del punto actual
    float distancia(Punto p1, Punto p2);
    //POST: devuelve el vector distancia p1-p2

private:

    float x_, y_;//coordenadas del punto actual

};

#endif // PUNTO_H

file: dot.cpp

#include "punto.h"
#include <cmath>

Punto::Punto()
{
    x_ = 0.0;
    y_ = 0.0;

}

Punto::Punto(float x, float y)
{
     x_ = x;
     y_ = y;
}

float Punto::abscisa()
{
    return x_;
}

float Punto::ordenada()
{
    return y_;
}

float Punto::distancia(Punto p1, Punto p2)
{
    float dis = sqrt(pow(2,((p1.x_) - (p2.x_))) + pow(2,((p1.y_)-(p2.y_))));

    return dis;
}

file: main.cpp HERE GIVES ME THE ERROR ...

#include <iostream>

#include "punto.h"




using namespace std;


int main()
{
    Punto s1(1.0,2.0);//Constructor dandole valores a las coordenadas
    Punto s2;// valores vacíos a las coordenadas
    float x = distancia(s1, s2);//ERROR 'distancia' was not declared in this scope
    cout << x << endl;
    return 0;
}

Use QT in linux mint to program. How is this error solved?

PS: I've already looked at this post and I do not it's the same error because I do have the distance function in the scope of the Point class.

    
asked by AguaSal 22.03.2017 в 01:03
source

1 answer

1

This is not what I would classify as TAD, but rather as Object Oriented Programming (OOP). The difference is that here the method is part of the definition of the class (in TAD they are separated).

Having said that:

1) If you have a point, the logical thing is that the operation distancia of the distance to another point, so you just pass a parameter (the other point, since you call the method from the starting point object) . So, float Punto::distancia(Punto otroPunto) 1 .

2) Since it is POO, the methods are invoked as part of an object. So the call would be float x = s1.distancia(s2);

1 And, if it's not a lot, I'd recommend that you use float Punto::distancia(Punto &otroPunto) ; the difference is that when you call a method defined with the version without the & , a new copy of the original object is passed through the parameter and with the version with & a reference is passed to the original object . In any case, I repeat, it's optional.

    
answered by 22.03.2017 / 02:00
source