i have two different operator overloading. For some reason it is giving error.
If i remove one of it, than it does not show any error. May i know why ?
Can i combine both ?
This is used for printing on screen.
ostream& operator<<(ostream &out, const Point &p) {
return out << "[" << setw(4) << p.getX() << setw(1) << "," << setw(4) << p.getY() << "] " << setprecision(3) << p.getScalarValue() << endl;
}
This is used for printing on a text file.
ofstream& operator<<(ofstream &out, const Point2D &p){
return out << "[" << setw(4) << p.getX() << setw(1) << "," << setw(4) << p.getY() << "] " << setprecision(3) << p.getScalarValue() << endl;
}
Error:
Point.cpp:91:147: error: invalid initialization of reference of type ‘std::ofstream& {aka std::basic_ofstream&}’ from expression of type ‘std::basic_ostream::__ostream_type {aka std::basic_ostream}’
You do not need the second version. You can use the first:
First, The
std::ostream& operator<<works for writing to files as well as writing to the standard output or stderrtSecond, assuming
Poind2Dinherits fromPoint, passing aPoint2Dto a function or operator that takes aPointreference will work too.