5

I get the following error and I'm not sure what the issue is

1 IntelliSense: "std::basic_ostream<_Elem, _Traits>::basic_ostream(const std::basic_ostream<_Elem, _Traits>::_Myt &_Right) [with _Elem=char, _Traits=std::char_traits]" (declared at line 82 of "C:\Program Files (x86)\Microsoft Visual Studio 11.0\VC\include\ostream") is inaccessible

Book.cpp

ostream operator<< (ostream& out, const Book & b){
    out << "Title: " << b.my_Title << endl;
    out << "Author: " << b.my_Author << endl;
    out << "Number of time checkout out: " << b.my_NumberOfTimesCheckedOut;
    return(out);
}

I get the issue with the return(out);

Book.h

#ifndef BOOK_H
#define BOOK_H
#include <string>
using namespace std;
namespace CS20A
{
    class Book {
    public:
        Book();
        Book( string author, string title );
        string getTitle() const;
        string getAuthor() const;
        int getNumberOfTimesCheckedOut() const;
        void increaseNumberOfTimesCheckedOut( int amount=1 );
        friend ostream operator<< ( ostream& out, const Book & b );
    private:
        string my_Author;
        string my_Title;
        int my_NumberOfTimesCheckedOut;
    };
};
#endif

I don't even understand what the error is telling me

CPPIssues
  • 53
  • 1
  • 4

2 Answers2

2

I suspect that you're using an ancient compiler that implements a prohibition on copying std::ostream, which is not copyable, by making its copy-constructor private; hence the confusing "inaccessible" error.

std::ostream is not copyable. You must return a reference:

ostream &operator<< (ostream& out, const Book & b){
Sam Varshavchik
  • 114,536
  • 5
  • 94
  • 148
  • But now I get an error on `friend ostream operator<< ( ostream& out, const Book & b );` saying > IntelliSense: cannot overload functions distinguished by return type alone – CPPIssues Jun 29 '16 at 02:48
  • I'm getting a lot of more random errors. But you fixed my problem thanks. – CPPIssues Jun 29 '16 at 03:01
0

I think you meant to return a reference of ostream.

ostream& operator<< (ostream& out, const Book & b){
    out << "Title: " << b.my_Title << endl;
    out << "Author: " << b.my_Author << endl;
    out << "Number of time checkout out: " << b.my_NumberOfTimesCheckedOut;
    return(out);
}

Even Better, you get to_string method like Java in new C++ versions.

Shubham Jain
  • 1,864
  • 2
  • 14
  • 24
  • Thank you! That fixed my error but now I have a new one =/ Error 5 error C2556: 'std::ostream &CS20A::operator <<(std::ostream &,const CS20A::Book &)' : overloaded function differs only by return type from 'std::ostream CS20A::operator <<(std::ostream &,const CS20A::Book &)' – CPPIssues Jun 29 '16 at 02:56
  • You need to add reference in your class declaration also - friend ostream& operator<< ( ostream& out, const Book & b ); – Shubham Jain Jun 29 '16 at 03:00