Thread: Insertion Operator overloading works on some objects but not on others

  1. #1
    Registered User
    Join Date
    Mar 2016
    Posts
    110

    Insertion Operator overloading works on some objects but not on others

    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;
    }
    Last edited by Vespasian_2; 10-23-2018 at 03:28 AM.

  2. #2
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,311
    This:
    Code:
    friend ostream& operator <<(ostream& out, Date& d){
    should have been:
    Code:
    friend ostream& operator <<(ostream& out, const Date& d){
    as the act of printing an object does not change its observable state.

    Also, this:
    Code:
    string GetPublisher(){
    should have been:
    Code:
    string GetPublisher() const{
    and likewise for GetTitle. For some reason you got it right for GetDate.
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  3. #3
    Registered User
    Join Date
    Mar 2016
    Posts
    110
    Thanks alot! I didnt know a const would cause a compilation error! I thought at most it was just to remind the reader that it is a value that should not change.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 5
    Last Post: 07-22-2015, 01:43 PM
  2. insertion operator
    By Aisthesis in forum C++ Programming
    Replies: 1
    Last Post: 05-23-2010, 04:42 AM
  3. Trouble with overloading Insertion Operator
    By adamdavis in forum C++ Programming
    Replies: 2
    Last Post: 01-23-2010, 06:25 PM
  4. overloading extraction and insertion
    By brianptodd in forum C++ Programming
    Replies: 3
    Last Post: 11-05-2003, 06:16 PM
  5. Overloading insertion and extraction problem
    By Curwa in forum C++ Programming
    Replies: 1
    Last Post: 01-15-2003, 09:20 PM

Tags for this Thread