0

I am currently learning C++ and I have this coding exercise where I am getting a segfault and I don't understand why. I have already tried using smart pointers, but I can't pass them into the Line class. I wouldn't know why there is a memory leak as I don't have created any variables on the heap.

#include <iostream>
#include <memory>
#include <ostream>
#include <sstream>

struct Point
{
  int x{ 0 }, y{ 0 };

  Point(){}

  ~Point(){}

  Point(const int x, const int y) : x{x}, y{y} {}

  Point(const Point &p) {}

  friend std::ostream& operator <<(std::ostream& os, const Point &p) {
      
      return os << "(" << p.x << ", " << p.y << ")";
  }
};

struct Line
{
  Point *start, *end;

  Line(Point* const start, Point* const end)
    : start(start), end(end)
  {
  }

  ~Line()
  {
    delete start;
    delete end;
  }

  Line deep_copy(const Line &other) const
  {
      Line new_line{other.start, other.end};

      return new_line;
  }

  friend std::ostream& operator <<(std::ostream& os, const Line &l) {
     
     return os << "(" << *l.start << ", " << *l.end << ")"; 

  }
};

int main()
{
    Point p1{0,0};
    Point p2{1,2};

    /* std::shared_ptr<Point> pp1 = std::make_shared<Point>(p1); */
    /* std::shared_ptr<Point> pp2 = std::make_shared<Point>(p2); */

    Line l1{&p1, &p2};

    Line l2 = l1;

    std::cout << l1 << std::endl;
    std::cout << l2 << std::endl;

    return 0;
}
neolith
  • 699
  • 1
  • 11
  • 20

0 Answers0