我收到以下错误,我不确定问题是什么
1智能感知:“std :: basic_ostream< _Elem, _Traits> :: basic_ostream(const std :: basic_ostream< _Elem,_Traits> :: _ Myt& _Right)[with _Elem = char,_Traits = std :: char_traits]“(声明 在“C:\ Program Files(x86)\ Microsoft Visual Studio”的第82行 11.0 \ VC \ include \ ostream“)无法访问
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);
}
我遇到了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
我甚至不明白错误告诉我的是什么
答案 0 :(得分:1)
我怀疑你正在使用一个古老的编译器,通过使其复制构造函数成为私有来实现禁止复制std::ostream
的禁止复制的std::ostream
;因此令人困惑的“难以接近的”错误。
ostream &operator<< (ostream& out, const Book & b){
不可复制。您必须返回参考:
{{1}}
答案 1 :(得分:0)
我认为你的意思是返回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);
}
甚至更好,你在新的C ++版本中得到像Java这样的to_string方法。