0

i'm doing some homework about OOP, i have to write 2 header files: Point.h and Triangle.h, and also define the methods in Point.cpp and Triangle.cpp.

I'm from Mexico so my code is a mix between english and spanish. When i run it, i get the following error:

PS C:\Users\ikerc\OneDrive\Escritorio\C++\TC1033\Clases\Act6> g++ main.cpp Point.cpp Triangle.cpp

In file included from Triangle.h:3, from main.cpp:3: Point.h:5:7: error: redefinition of 'class Point' 5 | class Point{ | ^~~~~ In file included from main.cpp:2: Point.h:5:7: note: previous definition of 'class Point' 5 | class Point{

Here are all the files that i used:

Point.h


#include<iostream>
#include<cmath>

class Point{

    public:
        Point(double, double);
        Point();
        double getX();
        double getY();
        double getDistance();
        void setX(double);
        void setY(double);
        double distance(Point);

    protected:
        double x;
        double y;
        double distancia;
      


};

Point.cpp

#include<iostream>
#include"Point.h"


Point::Point(double _x, double _y){
    x = _x;
    y = _y;
}

double Point::getX(){
    return x;
}

double Point::getY(){
    return y;
}

double Point::getDistance(){
    return distancia;
}

void Point::setX(double _x){
    x = _x;
}

void Point::setY(double _y){
    y = _y;
}

double Point::distance(Point Punto){
   
    distancia = sqrt(pow(Punto.getX() - x, 2) + pow(Punto.getY() - y, 2)); //Distancia entre puntos formula
    return distancia;
}

Triangle.h


#include<iostream>
#include"Point.h"

class Triangle: public Point{
    public:
        Triangle(Point, Point, Point);
        Triangle();
        double perimeter();
        double area();

    private:
        Point vertex1;
        Point vertex2;
        Point vertex3;
        double sideA;
        double sideB;
        double sideC;
        double areaT;
        double perimeterT;
};


Triangle.cpp

#include<iostream>
#include"Triangle.h"
#include<cmath>

Triangle::Triangle(Point _vertex1, Point _vertex2, Point _vertex3){
    vertex1 = _vertex1;
    vertex2 = _vertex2;
    vertex3 = _vertex3;
}

Triangle::Triangle(){
    vertex1.setX(0);
    vertex1.setY(0);
    vertex2.setX(50);
    vertex2.setY(30);
    vertex3.setX(25);
    vertex3.setY(10);
}

double Triangle::perimeter(){
    sideA = vertex1.distance(vertex2);
    sideB = vertex2.distance(vertex3);
    sideC = vertex3.distance(vertex1);


    perimeterT = sideA + sideB + sideC;
    return perimeterT;
}

double Triangle::area(){

     double s; 
     s = (perimeterT/2);
     areaT = sqrt(s * (s - sideA) * (s - sideB) * (s - sideC)); // Formula Heron
     return areaT;

}



main.cpp

#include<iostream>
#include"Point.h"
#include"Triangle.h"



int main(){
    Point Punto1(7, 5);
    Point Punto2(4, 1);
    Triangle Tr;

    Punto1.distance(Punto2);
    std::cout<<"\n\nLa distancia entre los puntos es: "<<Punto1.getDistance();
    std::cout<<"El area del triangulo es: "<<Tr.area();

    return 0;
}

I already tried using #pragma once at the top of Point.h, but it shows me this error: C:\Users\ikerc\AppData\Local\Temp\cchgOY8H.o:Triangle.cpp:(.text+0x22): undefined reference to `Point::Point()'

  • 4
    you need include gaurds – pm100 Nov 19 '22 at 02:03
  • 3
    the reason for the second error (when you do effectively have include guards), is because you do not have an implementation of `Point::Point()` – pm100 Nov 19 '22 at 02:04
  • You have two or more problems. `#pragma once` solved one of the problem, but once it did the build was able to get further and reveal the second problem. You have to be careful that you don't throw out a working solution to problem A simply because it exposes problem B. Sometimes you just have to trust that you've done the right thing and plod on through the newly discovered errors, and sometimes this trust only comes through experience. – user4581301 Nov 19 '22 at 02:14

0 Answers0