This is because you did not make your Point
a const
when declaring and defining your operator. Change your declaration as follows:
friend ostream &operator<<(ostream&, const Point&);
Also add const
in the definition:
ostream &operator<<(ostream &out, const Point &p){
out<<"("<<p.x<<", "<<p.y<<", "<<p.z<<")\n";
return out;
}
Note that the code that you posted does not require const
-ness of the Point&
. Some other code made your compiler or IDE believe that an operator with a const
is referenced. For example, using the operator like this would require a const
cout << Point(1.2, 3.4, 5.6) << endl;
Since the snippet above creates a temporary object, passing a reference to it as a non-const is prohibited by the C++ standard.
Not directly related to this issue, but you may want to mark the three getters for individual coordinates const
as well:
double get_x() const {return x;}
double get_y() const {return y;}
double get_z() const {return z;}
This would let you access coordinates with getters on objects marked const
.