Help, please, to understand the program. I study C ++ and have reached operator overload. I need to do the following: implement the Date class, which will store the day, month, and year, initialize this class, and also overload the << operator to display this date in the format (1, 1, 2000). The class I implemented and overload too, but gives an error and I can not help it.

PS Operator overloading needs to be implemented in a class, not outside (if possible)

The code itself: `

class Date { int day; int month; int year; public: Date(int d, int m, int y) : day {d}, month {m}, year {y} { } int getDay() { return day; } int getMonth() { return month; } int getYear() { return year; } ostream &operator<<(ostream &os) { return os << '(' << getDay() << '.' << getMonth() << '.' << getYear() << ')' << endl; } }; int main() { Date dd {10, 10, 2010}; cout << dd; return 0; } 

`

Mistake:
In function 'int main()': error: no match for 'operator<<' (operand types are 'std::ostream {aka std::basic_ostream<char>}' and 'Date') cout << dd; ~~~~~^~~~~

    2 answers 2

    You have created an operator that works the other way around :)

     dd << cout; 

    This is how it should be - the operator should be a free function:

     class Date { int day; int month; int year; public: Date(int d, int m, int y) : day {d}, month {m}, year {y} { } int getDay() const { return day; } int getMonth()const { return month; } int getYear() const { return year; } }; ostream& operator<<(ostream &os, const Date& d) { return os << '(' << d.getDay() << '.' << d.getMonth() << '.' << d.getYear() << ')' << endl; } int main() { Date dd {10, 10, 2010}; cout << dd << endl; return 0; } 
    • Tell me, how can I solve this problem in order to display correctly, not vice versa? - Simple User
    • See Augmented Reply - Harry
    • Is this overload possible in the class itself? Not Beyond - Simple User
    • You can declare him a friend and contribute to the class (see answer @Alexander), but this will NOT make it implemented in the class. This is a non-member free function. If you make a statement a member of a class, then the class object should go first - check for yourself, in your version of dd << cout will work. Just like if you make the definitions of get... from a class declaration, they will not become free functions, but remain members of the class. - Harry
    • Yes, as you described, it works. Another small question: if you implement 2 or 3 extra. class, is it possible for everyone to realize their overload << or not? - Simple User
     #include <iostream> #include <ostream> using namespace std; class Date { int day; int month; int year; public: Date(int d, int m, int y) : day {d}, month {m}, year {y} { } int getDay() const { return day; } int getMonth() const { return month; } int getYear() const { return year; } friend ostream &operator<<(ostream &os, const Date &_date) { return os << '(' << _date.getDay() << '.' << _date.getMonth() << '.' << _date.getYear() << ')' << endl; } }; int main() { Date dd {10, 10, 2010}; cout << dd; return 0; } 
    • And why should he be a friend if he does not use private members? - Harry
    • @Harry, someone as you like, you can free function, you can friend-function. - Alexander
    • Just scattering your friends is unhealthy. You give this operator full access to all entrails! - Harry
    • @Harry, in this case it does not affect anything. - Alexander
    • one
      Well, firstly, the class may be revised in the future, and secondly, it creates a bad habit :) - Harry