Thread: ostream<< overloading for bookinventory program

  1. #1
    Registered User
    Join Date
    Jul 2003
    Posts
    450

    ostream<< overloading for bookinventory program

    I am getting an error in my << overloaded function of my book class that I do not understand here is some code snippits:
    Code:
    void Book::printAuthors(const vector<Author> & tempauthors){
        vector<Author>::const_iterator constIterator;
        
        for (constIterator=tempauthors.begin();
             constIterator!=tempauthors.end();
             ++constIterator)
             cout << *constIterator<<endl;
    }
    
    ostream &operator<<(ostream & os,const Book & tBook){
        os << "\nTitle: "<<tBook.get_title()<<'\n';
        os << "Publisher: "<<tBook.get_publisher()<<'\n';
        os << "Authors: "<<'\n';
        tBook.printAuthors(tBook.authors);
        
        os << endl;
        return os;
    }
    the errors:
    ook.cpp: In function `std::ostream& operator<<(std::ostream&, const Book&)':
    book.cpp:90: passing `const Book' as `this' argument of `void
    Book::printAuthors(const std::vector<Author, std::allocator<Author> >&)'
    discards qualifiers

  2. #2
    Registered User
    Join Date
    May 2003
    Posts
    161
    You're calling a non-const method on a const object. Change your printAuthors method to:

    Code:
    void Book::printAuthors(const vector<Author> & tempauthors) const {

  3. #3
    Registered User
    Join Date
    Jul 2003
    Posts
    450
    thanks for the input that fixed the problem

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Need help with a program, theres something in it for you
    By engstudent363 in forum C Programming
    Replies: 1
    Last Post: 02-29-2008, 01:41 PM
  2. Replies: 4
    Last Post: 02-21-2008, 10:39 AM
  3. Using variables in system()
    By Afro in forum C Programming
    Replies: 8
    Last Post: 07-03-2007, 12:27 PM
  4. My program, anyhelp
    By @licomb in forum C Programming
    Replies: 14
    Last Post: 08-14-2001, 10:04 PM