c++ - Understanding ostream overload -
i'm having problem understanding error receive when overloading ostream method class.
code in class.cpp
ostream& operator<<(ostream& out, const datetype& d) { out << d.getyear() << "-" << d.getmonth() << "-" << d.getday() homecoming out; } i know 3 getters work, test them in main.cpp.
however, when run like:
cout << d1 << endl: i error:
‘std::ostream& datetype::operator<<(std::ostream&, datetype&)’ must take 1 argument ostream& operator<<(ostream&, datetype&); i wrote ostream code programme works fine. why error here?
you wrote fellow member function of datetype, , fellow member operator<< may take 1 explicit argument (because first 1 implicit , operator binary). @ moment have sort of three-argument operator<<, taking implicit datetype, std::ostream&, datetype!
here's how fellow member operator<< look:
struct t { operator<<(ostream&); }; the problem have operator<< takes t on left , stream on right, reverse convention. t() << std::cout isn't right, it?
conventionally, then, utilize namespace-scope our operator<< overloads, have total command on parameter order.
that is, don't create fellow member function of datetype. may need create new function friend of datetype if getters private.
i wrote ostream code programme works fine.
no you/it didn't.
c++
No comments:
Post a Comment