2012-10-11 46 views
0

我有下面的代碼,它不斷給我錯誤,我不確定該怎麼做,因爲我有運算符聲明。在C++類中的運算符

#ifndef BOOK_H 
#define BOOK_H 
#include<vector> 
#include<string> 
using namespace std; 
class Book { 
public: 
    Book(); 
    Book(vector<string>*, string, int); 
    Book(const Book&); 
    ~Book(); 
    Book& operator=(const Book&); 
    void update(vector<string>*); 
    void update(string); 
    void update(int); 
    int getYear() const{ 
     return year; 
    }; 
    string getTitle() const{ 
     return title; 
    }; 
    bool operator==(const Book&); 
    bool operator!=(const Book&); 
    friend ostream& operator<<(ostream&, const Book&); 
    void getAuthors(); 
private: 
    vector<string>* authors; 
    string title; 
    int year; 
}; 

#endif 


#include "Book.h" 
#include <vector> 
#include <string> 
#include <cstdlib> 
using namespace std; 
Book::Book():year(0), title(NULL), authors(NULL){} 
Book::Book(vector<string>* bookauthors,string booktitle, int bookyear){ 
    authors = bookauthors; 
    title = booktitle; 
    year = bookyear; 
} 
Book::Book(const Book& aBook){ 
    authors = aBook.authors; 
    title = aBook.title; 
    year = aBook.year; 
} 
bool Book::operator==(const Book &aBook){ 
    if(getYear() == aBook.getYear() && getTitle() == aBook.getTitle()) 
     return true; 
    else return false; 
} 
bool Book::operator != (const Book &aBook){ 
    if(getYear() != aBook.getYear() && getTitle() == aBook.getTitle()) 
     return true; 
    else return false; 
} 
Book& Book::operator =(const Book& rhs){ 
    if(this != rhs){ 
     authors = rhs.authors; 
     title = rhs.title; 
     year = rhs.year; 
    } 
    return *this; 
} 
void Book::update(int newyear){ 
    year = newyear; 
} 
void Book::update(string newtitle){ 
    title = newtitle;  
} 
void Book::update(vector<string>* newauthors){ 
    authors = newauthors; 
} 
ostream& operator<<(ostream& os, const Book& b){ 
    os<<b.getTitle()<<"/"<<b.getYear(); 
    return os; 
} 

和我不斷收到此錯誤,我不知道如何解決它。

Book.cc: In member function 'Book& Book::operator=(const Book&)': 
Book.cc:28: error: no match for 'operator!=' in 'this != rhs' 
Book.cc: In function 'std::ostream& operator<<(std::ostream&, const Book&)': 
Book.cc:45: error: no match for 'operator<<' in 'std::operator<< [with _CharT = char, _Traits = std::char_traits<char>, _Alloc = std::allocator<char>](((std::basic_ostream<char, std::char_traits<char> >&)((std::basic_ostream<char, std::char_traits<char> >*)os)), ((const std::basic_string<char, std::char_traits<char>, std::allocator<char> >&)((const std::basic_string<char, std::char_traits<char>, std::allocator<char> >*)(& Book::getTitle() const())))) << "/"' 
Book.cc:44: note: candidates are: std::ostream& operator<<(std::ostream&, const Book&) 

感謝您的幫助。

+0

你爲什麼要檢查自我分配? –

+0

@JohnDibling也許是因爲「The C++ programming language」一書,或者說「Effective C++ one」。它可能已經變得不合時宜,但它仍然是有信譽的來源提供的建議。 – juanchopanza

回答

1

對於第一個錯誤,您將this(它是一個Book指針)與Book引用進行比較。你需要比較this的參數的地址,即比較指針:

if(this != &rhs){ .... } 

關於第二個錯誤,我認爲沒有錯的,你發佈的代碼。但是,您不需要將ostream& <<運營商聲明爲好友,因爲它只能訪問公共方法。

您還應該讓您的比較運算符==!=const,儘管這與您報告的錯誤無關。

+0

這只是比較指針。它不會調用類的操作符!=。 –

+2

@GrahamPerks的權利,這正是避免自我分配所需要的。 – juanchopanza

+0

啊,賓果,你是對的。 –

3

更換

bool operator==(const Book&); 
bool operator!=(const Book&); 
friend ostream& operator<<(ostream&, const Book&); 

通過

bool operator==(const Book&) const; 
bool operator!=(const Book&) const; 
friend std::ostream& operator<<(std::ostream&, const Book&); 

而平等檢查時,儘量

if (this != &rhs) 

if (*this != rhs) 

取決於您是要進行深層還是淺層比較。

+0

他想在那裏進行淺層比較,這是一個常見的(壞)成語。 –

+0

恕我直言做比較是一個壞主意,請參閱[這裏這個流行的答案](http://stackoverflow.com/a/9322542/728847) – marton78

+0

因此,自我分配是好的嗎? – juanchopanza