The exercise consists of implementing a Point TAD, with the operations abscissa (returns x), ordinate (returns 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 IT GETS 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;
}
I use QT on linux mint for programming. How to fix this error?
PS: I already looked at this post and it is not the same error because I do have the distance function in the scope of the Point class.
This is not what I would classify as ADT, but as Object Oriented Programming (OOP). The difference is that here the method is part of the class definition (in TAD they are separate).
Having said that:
1) If you have a point, the logical thing is that the operation
distancia
gives you the distance to another point, so you only pass it one parameter (the other point, since you call the method from the initial point object). So,float Punto::distancia(Punto otroPunto)
1 .2) Since it is OOP, the methods are called as part of an object. So the call would be
float x = s1.distancia(s2);
1 And, if it's not too much of a hassle, I would recommend that you use
float Punto::distancia(Punto &otroPunto)
; the difference is that when calling a method defined with the version without , a new copy of the original object&
is passed as a parameter and with the version with , a reference to the original object is passed . In any case, I repeat, it is optional.&