用C ++覆盖类和运算符

时间:2012-10-16 12:23:52

标签: c++ class operators override

我正在将这个课程扩展为有声读物:

#include "Book.h"
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;
}
Book::~Book(){
    //delete authors;
    //delete &title;
    //delete &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;
}
/*void Book::getAuthors()const{
    for(int i = 0; i < authors->size(); i++){
        cout<<authors->at(i);
    }
}*/
std::ostream& operator <<(std::ostream& os, const Book& b){
    os<<b.getTitle()<<", "<<b.getYear();
    for(int i = 0; i < b.authors->size();i++){
        os<<",  "<<b.authors->at(i);
    }
    return os;
}

我不断收到这个有声读物课的错误:

#include "AudioBook.h"
AudioBook::AudioBook():Book(),length(0){}
AudioBook::AudioBook(vector<string>* authors1, string title1, int year1, double length1){
    Book(authors1, title1, year1);
    length = length1;
}
AudioBook::AudioBook(const AudioBook& other):Book(other),length(other.length){}

AudioBook& AudioBook::operator =(const AudioBook& rhs){
    Book::operator =(rhs);
    length = rhs.length;
}
AudioBook::~AudioBook(){

}
bool AudioBook::operator !=(const AudioBook& aBook){
    Book::operator !=(aBook);
    if(length == aBook.length)
        return false;
    else
        return true;
}
void AudioBook::update(double newlength){
    length = newlength;
}
/*std::ostream& operator<<(ostream& os, const AudioBook& aBook){
    Book::operator<<(os, aBook);
    os<<"length: "<<aBook.getLength()<<;
}*/

这就是我得到的:libc ++ abi.dylib:terminate调用抛出异常

我不确定它是否覆盖了运营商或者是什么?我怎么能覆盖运算符&lt;

1 个答案:

答案 0 :(得分:0)

AudioBook& AudioBook::operator =(const AudioBook& rhs)缺少return *this;

不确定这是否是原因。