Hi,
I'm trying to overload the << operator. I have a point class which is represented by a vector of doubles. In this class I have overloaded the << operator with no problem:
Now I have created another class, which instantiates 4 points of class Point:
When I try to overload the << operator here I get a segmentation fault:
The function get_1() just returns the first point:
Can anybody tell what I'm doing wrong? It fails when I try to print the coordinate in the point. The output is "1. point: (" and nothing more
I'm trying to overload the << operator. I have a point class which is represented by a vector of doubles. In this class I have overloaded the << operator with no problem:
Code:
class Point
{
public:
//constructor
Point();
Point(vector<double> v);
}
Point::Point()
{
vector<double> vec(4,0);
vec[3]= 1;
point = vec;
size = vec.size();
}
Code:
ostream& operator<<(ostream& os, const Point& p)
{
int sz = p.get_size();
os << "(";
for (int i=0; i<sz; i++) {
os << p.get_val(i) << " ";
}
os << ")" << endl;
return os;
}
Code:
fourpoints::fourpoints()
{
Point p1;
Point p2;
Point p3;
Point p4;
}
Code:
ostream& operator<<(ostream& os, const fourpoints& p)
{
os << "1. point: " << p.get_1();
return os;
}
Code:
Point fourpoint::get_1() const
{
return this->p1;
}
Comment