1
我有我的operator<<
超載的問題,我不能訪問類是在不管我做什麼,因爲它會說,這些變量是私有的的私有變量編譯器錯誤。這是我當前的代碼:朋友運算符重載<<問題,
#include "library.h"
#include "Book.h"
using namespace cs52;
Library::Library(){
myNumberOfBooksSeenSoFar=0;
}
//skipping most of the functions here for space
Library operator << (ostream &out, const Library & l){
int i=myNumberOfBooksSeenSoFar;
while(i<=0)
{
cout<< "Book ";
cout<<i;
cout<< "in library is:";
cout<< l.myBooks[i].getTitle();
cout<< ", ";
cout<< l.myBooks[i].getAuthor();
}
return (out);
}
和函數原型和私有變量在library.h
是
#ifndef LIBRARY_H
#define LIBRARY_H
#define BookNotFound 1
#include "Book.h"
#include <iostream>
#include <cstdlib>
using namespace std;
namespace cs52{
class Library{
public:
Library();
void newBook(string title, string author);
void checkout(string title, string author) {throw (BookNotFound);}
void returnBook(string title, string author) {throw (BookNotFound);}
friend Library operator << (Library& out, const Library & l);
private:
Book myBooks[ 20 ];
int myNumberOfBooksSeenSoFar;
};
}
#endif
我知道應該這樣,這就是爲什麼我很困惑,因爲它仍然說myNum的......是在編譯私人。如果有幫助,這裏是錯誤代碼。 (cs52 :: Library operator <<(std :: ostream&,const cs52 :: Library&)':| C:\ Users \ Devin \ Documents \ C++ SMCCLASS \ LibrarySystem \ library.h | 23 |錯誤:'int cs52 :: Library :: myNumberOfBooksSeenSoFar'是private | –
@Devin:你確定嗎?你的函數原型不正確的開始。 –
@Devin:檢查更新的答案,我認爲這是你的問題。注意參數類型! –