I'm getting the following output:
[1, 1] Segmentation fault
Any ideas what's causing the segmentation fault?
#include <iostream>
using namespace std;
// point class
class Point {
friend std::ostream &operator<<( ostream &, const Point &);
public:
Point (int = 0, int = 0);
int getX() const {return x;};
int getY() const {return y;};
void setX(int _x) {x = _x;};
void setY(int _y) {y = _y;};
Point &operator+(const Point& p) {
Point p_self;
p_self.setX(x + p.getX());
p_self.setY(y + p.getY());
// suspect this line is causing the segmentation fault
return p_self;
}
protected:
int x, y;
};
Point::Point (int a, int b) {x = a; y = b;}
std::ostream &operator<<(ostream &output, const Point &p) {
output << '[' << p.x << ", " << p.y << ']';
return output;
}
int main() {
Point p = Point(1, 1);
cout << p << endl;
Point p2 = p + p;
cout << p2 << endl;
return 0;
}
I'm getting the following output:
[1, 1] Segmentation fault
Any ideas what's causing the segmentation fault?