【问题标题】:operators in C++ classC++ 类中的运算符
【发布时间】:2012-10-11 18:12:57
【问题描述】:

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

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

感谢您的帮助。

【问题讨论】:

  • 你为什么要检查自分配?
  • @JohnDibling 可能是因为那本《C++ 编程语言》一书,或者那本《有效的 C++ 》。它可能已经过时,但它仍然是知名人士提供的建议。

标签: c++ class operators


【解决方案1】:

替换

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 popular answer here
  • 所以可以自行分配?
  • 这是一个品味问题。设计良好的代码在自分配时不应该中断,所以这只是一个性能问题。通常,自分配是例外而不是规则,因此要优化常见情况,您可以跳过检查。
  • @marton78:这不是品味问题——检查自分配是糟糕的代码。任何像x = x; 这样的代码显然都被破坏了。掩饰就是无视存在的任何问题并继续进行,就好像什么都没发生过一样。出于同样的原因,为什么全局 catch (...) 不好,这个也是。不要假装问题不存在。修复代码。
【解决方案2】:

对于第一个错误,您将this(一个Book 指针)与Book 引用进行比较。您需要将this 与参数的地址进行比较,即比较指针:

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

关于第二个错误,我在您发布的代码中没有发现任何问题。但是,您不需要将 ostream&amp; &lt;&lt; 运算符声明为友元,因为它只访问公共方法。

您还应该将比较运算符设为== 和!= const,尽管这与您报告的错误无关。

【讨论】:

  • 那只会比较指针。它不会调用类的运算符!=.
  • @GrahamPerks 对,这正是避免自我分配所需要的。
猜你喜欢
  • 1970-01-01
  • 2011-05-04
  • 2011-12-04
  • 1970-01-01
  • 2011-11-29
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多