C++类中的运算符
operators in C++ class
我有以下代码,它一直给我错误,我不确定该怎么做,因为我已经声明了运算符。
#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&)
感谢您的帮助。
替换
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)
取决于你是要做深度比较还是浅层比较。
对于第一个错误,您正在将this
(Book
指针)与Book
引用进行比较。您需要将this
与参数的地址进行比较,即比较指针:
if(this != &rhs){ .... }
关于第二个错误,我认为您发布的代码没有任何错误。但是,不需要将ostream& <<
运算符声明为友元,因为它仅访问公共方法。
您还应该将比较运算符设为 ==
和 !=
const
,尽管这与您报告的错误无关。
相关文章:
- 为什么比较运算符如此快速
- C++映射:具有自定义类的运算符[]不起作用(总是返回0)
- 使用C++中的模板和运算符重载执行矩阵运算
- 为什么这个运算符<重载函数对 STL 算法不可见?
- 增量运算符与后缀混淆
- 一个关于在C++中重载布尔运算符的问题
- 运算符C++ "delete []"仅删除 2 个前值
- 模板类无法识别友元运算符
- 我可以使用条件运算符初始化C风格的字符串文字吗
- 关闭||运算符优化
- 通过继承类使用来自不同命名空间的运算符
- C++Cast运算符过载
- 如何使用AngelScript注册SFML Vector2运算符
- 重载元组索引运算符-C++
- 如何使用重载的相等(==)运算符向测试用例添加描述
- 为什么Mat类的两个对象可以在不重载运算符+的情况下添加
- 多个If语句与使用逻辑运算符计算条件的单个语句的比较
- 布尔比较运算符是如何在C++中工作的
- 重载运算符new[]的行为取决于析构函数
- 如何防止clang格式在流运算符调用之间添加换行符<<