c++ 模板类中的自定义集合比较函数

jyztefdp  于 2023-01-28  发布在  其他
关注(0)|答案(1)|浏览(160)

我试着从一次学校考试中找出这个练习。他们实现了一个抽象模板Book类,任务是实现一个书架类。我试着用一个自定义比较器构造一组book指针,但后来我遇到了一个编译错误:

In template: reference to type 'const Book<std::basic_string<char>>' could not bind to an lvalue of type 'const std::_Rb_tree<...>

(我实现了一个子类BOOK2,只是为了调试)
这是一个长给定的图书抽象类

#include <iostream>
#include <set>
#include <string>
#include <utility>

template <class T>
class Book
{
  // any member variables are inaccessible to descendants
 private:
  std::string _title; // do not call a copy-ctr
  T _author; // do not call a copy-ctr
  size_t _number_of_pages;
 public:
  Book(std::string title,
       T author,
       size_t number_of_pages)
      : _title(std::move(title)),
        _author(std::move(author)),
        _number_of_pages(number_of_pages)
  {}

  virtual ~Book() = default;

  const std::string& get_title() const
  { return _title; }

  const T& get_author()  const
  { return _author; }

  size_t get_number_of_pages() const
  { return _number_of_pages; }

 public:
  virtual Book<T>* clone() const = 0; // implemented *only* by descendent classes

  virtual bool is_available_on(const std::string& platform) const = 0; // implemented *only* by descendant classes

 protected:
  virtual void do_output(std::ostream& os) const // can be overridden; can be accessed *only* by descendants
  {
    os << _title << ", " << _author << ", " << _number_of_pages << " pages";
  }

  // output should depend on who book really is
  friend std::ostream& operator<<(std::ostream& os, const Book& book)
  {
    book.do_output(os);
    return os;
  }
};

这是我实现的:

class Book2: public Book<std::string>{
 public:
  Book2(std::string &title,
        std::string &author,
        size_t number_of_pages)
      : Book<std::string>(title,author,number_of_pages){}

  bool is_available_on(const std::string &platform) const override{return
        true;}
  Book<std::basic_string<char>> * clone() const override{
    Book<std::basic_string<char>> * a{};
    return  a;
  }
};

template<class TP>
static bool book_comp(const Book<TP>& a,const Book<TP> & b){
  return a.get_title()<b.get_title();}

template<class TT>
class Bookshelf
{
 public:
  typedef bool(*book_comp_t)(const Book<TT>& a,const Book<TT> & b);
  // DO NOT CHANGE NEXT TWO LINES:
  auto& get_books() { return _books; } // DO NO CHANGE
  auto& get_books() const { return _books; } // DO NO CHANGE

  Bookshelf():_books(book_comp<TT>){}

  void add(Book<TT>& book)
  {
    size_t init_size=_books.size();
    _books.insert (&book);
    if(init_size==_books.size()){
      throw std::invalid_argument("book already in bookshlf");
    }
  }

  // sorted lexicographically by title
  friend std::ostream& operator<<(std::ostream& os, const Bookshelf<TT>&
      bookshelf)
  {
    for(const auto& book :bookshelf._books)
    {
      os << *book << std::endl;
    }
  }

 private:
  std::set<Book<TT>*,book_comp_t> _books;
};

int main ()
{
  std::string  a ="aba";
  std::string bb ="ima;";
  Book2 b = Book2(a, bb, 30);
  Bookshelf<std::string> shelf;
  std::cout<<b;
  shelf.add(b);
}

我试过在某些地方修改常量限定符,但没有成功。我也试过不使用自定义比较器函数,但运行正常。我想这可能是一些语法错误?

wxclj1h5

wxclj1h51#

std::set<Book<TT>*,book_comp_t> _books;Book<TT>*的集合,因此需要参数类型为Book<TT>*而不是const Book<TT>&的比较器

相关问题