Hi all,
I have overloaded an insertion operator which works on the object within which it is defined (see line 47).
But when I use this insertion operator on an object that contains the other object within which the operator is defined, I get a compilation error "no match for operator <<" on line 50.
What I mean is, calling the overloaded operator on an object within which it is defined works. But calling it on a statement which returns that obejct (line 50) doesnt work. Which is strange because the statement on line 50 looks to me like it returns a Date object and that it should work.
Any clues as to how to fix this?
Code:
#include<iostream>
using namespace std;
class Date{
public:
friend ostream& operator <<(ostream& out, Date& d){
out << d.day << "/" << d.month << "/" << d.year << endl;
}
Date(int y, int m, int d) : year(y), month(m), day(d){
};
private:
int year, month, day;
};
class Publication{
public:
Publication(const string& p, const Date& d, const string& t) : publisher(p), date(d), title(t){
};
Date GetDate() const {
return date;
};
string GetPublisher(){
return publisher;
};
string GetTitle(){
return title;
};
private:
string publisher;
Date date;
string title;
};
int main (){
Date date1(2018,10,23);
cout << date1;//THIS WORKS
Publication publication1("MegaPublishers", date1, "HowToBeAMillionare");
cout << publication1.GetPublisher();//THIS ALSO WORKS
cout << publication1.GetDate();//BUT THIS DOESNT
return 0;
}