Thread: Overloading the << operator

  1. #1
    Registered User
    Join Date
    Mar 2007
    Posts
    92

    Overloading the << operator

    I need to overload the << operator as a virtual function. I've only ever done them as friend functions and friend functions can't be virtual functions so I'm running into problems.

    In the base class I'm using it as a pure virtual function:

    Code:
    //header
    
    virtual void operator<<(ostream &, const Holding &) = 0;
    and in the derived class I'm using it like this
    Code:
    //header
    
    virtual void operator<<(ostream &, const Book &);
    Code:
    //.cpp
    
    void Book::operator<<(ostream &out, const Book &book){
            
            book.print(out);
            
            }
    I'm getting these errors from it:

    `void Book::operator<<(std::ostream&, const Book&)' must take exactly one argument
    and
    In member function `virtual void Book::operator<<(std::ostream&, const Book&)':
    and
    `const Book' as `this' argument of `virtual void Book::print(std::ostream&)' discards qualifiers
    and
    cannot allocate an object of type `Book'
    because the following virtual functions are abstract:
    virtual void Holding::operator<<(std::ostream&, const Holding&)


    What do you think might be the problem?

  2. #2
    Registered User
    Join Date
    Mar 2007
    Posts
    92
    I was able to figure it all out

  3. #3
    Registered User
    Join Date
    Jan 2005
    Posts
    7,366
    Feel free to post the solution you found. As I'm sure you noticed, operator<< cannot be a member function, and so it cannot be virtual. Here is more reading for those interested:

    http://www.parashift.com/c++-faq-lit...html#faq-15.11

  4. #4
    Registered User
    Join Date
    Mar 2007
    Posts
    92
    Well I got it to compile and then I checked it different ways and it's not working like it's supposed to. It's not even actually calling the function when I try to output it. I'll read that article and see if I can figure it out. Thanks.

  5. #5
    Registered User
    Join Date
    Mar 2007
    Posts
    92
    I'm still getting this error
    Code:
    `const Holding' as `this' argument of `virtual void Holding::print(std::ostream&)' discards qualifiers
    I have it set up pretty much the same way as that link you showed. My print function in the Holding class is a pure virtual function. I'm not sure if that is screwing this up or not. Also what does it mean to discard qualifiers?
    Last edited by warfang; 05-07-2007 at 06:54 PM.

  6. #6
    Registered User
    Join Date
    Mar 2007
    Posts
    92
    For anyone who is interested I found an answer.

    The standard library includes a templated functional called less. less is a
    struct that has an operator() that takes two arguments and applies < to
    them. The two arguments are declared as const, which means that it is not
    permissible to call any operator on them which is not likewise const.
    Calling a non-const operator "discards qualifiers", i.e., discards const.
    I just had to make my print functions const functions.

  7. #7
    Registered User
    Join Date
    Mar 2007
    Posts
    92
    This just allowed me to compile it. When I actually call the overloaded << operator it just returns the memory address the object is at. I'm trying to print out some of the data it holds .

  8. #8
    Registered User
    Join Date
    Jan 2005
    Posts
    7,366
    What does your print function look like? What does the code that calls it look like?

  9. #9
    Registered User
    Join Date
    Mar 2007
    Posts
    92
    Code from the Derived class

    Code:
    void Book::print(ostream & out)const{
            
            out << "BOOK: " << author << " " << '\"' << title << '\"' << " " << callNumber << endl;
            
            }  // This is a virtual function
    ostream & operator<<(ostream & out, const Book & book){
         
         book.print(out);
         return out;
         
         } // This is a friend function
    The code that calls it looks like this

    Code:
        Book * book;
        Recording * recording;
        Holding * Holdings[5];
        .
        .
        getting variable data
        .
        .
        book = new Book(author, title, callNumber);
        Holdings[i] = book;  // Holdings is an ABC
    
        cout << Holdings[i] << endl;
        // cout << book << endl;   (they both have the same result)
    Last edited by warfang; 05-07-2007 at 08:06 PM.

  10. #10
    Registered User
    Join Date
    Mar 2007
    Posts
    92
    Should I post more of my code or is that sufficient?

  11. #11
    Registered User
    Join Date
    Mar 2007
    Posts
    92
    Wow once again I feel dumb. I fixed it. I was getting a memory reference because I wasn't passing in a pointer. Now I am and it works. Saweet!!!

  12. #12
    Registered User
    Join Date
    Jan 2005
    Posts
    7,366
    Usually you would fix it the other way around. Instead of changing the function to take a pointer, you would leave the function to take a reference and pass a reference instead of a pointer. If you have a pointer and you want to pass a reference, just dereference it:
    Code:
    cout << *(Holdings[i]) << endl;
    cout << *book << endl;

  13. #13
    Registered User
    Join Date
    Mar 2007
    Posts
    92
    That is what I did. I guess I didn't make it clear. I didn't change the function. The function always received a constant reference but I wasn't passing in a pointer. Now I am. Thanks for all the help!

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. unary operator overloading and classes
    By coletek in forum C++ Programming
    Replies: 9
    Last Post: 01-10-2009, 02:14 AM
  2. Smart pointer class
    By Elysia in forum C++ Programming
    Replies: 63
    Last Post: 11-03-2007, 07:05 AM
  3. Overloading the << operator.
    By antex in forum C++ Programming
    Replies: 5
    Last Post: 05-31-2005, 01:37 AM
  4. overloading the output operator <<
    By neandrake in forum C++ Programming
    Replies: 9
    Last Post: 12-07-2004, 02:25 PM
  5. operator overloading and dynamic memory program
    By jlmac2001 in forum C++ Programming
    Replies: 3
    Last Post: 04-06-2003, 11:51 PM