gpt4 book ai didi

c++ - C++ 类中的运算符

转载 作者:行者123 更新时间:2023-11-27 23:21:14 25 4
gpt4 key购买 nike

我有以下代码,它一直给我错误,我不确定该怎么做,因为我声明了运算符。

#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)

取决于你是想做深度比较还是浅层比较。

关于c++ - C++ 类中的运算符,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/12845961/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com