重写C++中的类和运算符
Overriding classes and operators in C++
我正在将这个类扩展到有声读物中:
#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:终止称为抛出异常
我不确定是运算符的覆盖还是什么? 还有我怎样才能覆盖运算符<</div>
缺少AudioBook& AudioBook::operator =(const AudioBook& rhs)
return *this;
不确定这是否是原因。
相关文章:
- 为什么比较运算符如此快速
- C++映射:具有自定义类的运算符[]不起作用(总是返回0)
- 使用C++中的模板和运算符重载执行矩阵运算
- 为什么这个运算符<重载函数对 STL 算法不可见?
- 增量运算符与后缀混淆
- 一个关于在C++中重载布尔运算符的问题
- 运算符C++ "delete []"仅删除 2 个前值
- 模板类无法识别友元运算符
- 我可以使用条件运算符初始化C风格的字符串文字吗
- 关闭||运算符优化
- 通过继承类使用来自不同命名空间的运算符
- C++Cast运算符过载
- 如何使用AngelScript注册SFML Vector2运算符
- 重载元组索引运算符-C++
- 如何使用重载的相等(==)运算符向测试用例添加描述
- 为什么Mat类的两个对象可以在不重载运算符+的情况下添加
- 多个If语句与使用逻辑运算符计算条件的单个语句的比较
- 布尔比较运算符是如何在C++中工作的
- 重载运算符new[]的行为取决于析构函数
- 如何防止clang格式在流运算符调用之间添加换行符<<