c++ - 使用自定义类的 std::sort 错误

标签 c++ sorting

我浏览了 stackoverflow 并添加了一个重载运算符,希望能够使其与排序一起使用。尽管我仍然收到大量错误,指出排序有问题。

代码:

#include <iostream>
#include <vector>
#include <ctime>
#include <cstdlib>
#include <algorithm>

class Student
{
private:
  std::string name_;
  int number_;
  std::vector<int> grades_;
  const int num_courses_;

  static std::string gen_name() {
    return std::to_string(rand());
  }
  static int gen_number() {
    return rand() % (201600000 - 201100000 + 1) + 201100000;
  }
  static int gen_grade() {
    return rand() % (100 - 70 + 1) + 70;
  }
  double compute_average() {
    int marks = 0;
    int count = 0;
    for (std::vector<int>::iterator i = grades_.begin(); i != grades_.end(); i++, count++){
      marks += *i;
    }
    return static_cast<double>(marks) / count;
  }

public:
  Student() : name_(gen_name()), number_(gen_number()), num_courses_(5)
  {
    for (int i = 0; i < num_courses_; ++i) {
      grades_.push_back(gen_grade());
    }
  }

  double getAvg() {
    return compute_average();
  }
  friend std::ostream& operator<<(std::ostream& os, Student& s) {
    os << "Name = " << s.name_ << "\tNumber = " << s.number_ << "\tAvg = " << s.compute_average();
    return os;
  }

  std::string getName() {
    return name_;
  }
  void print_grades(std::ostream& os) const
  {
    for (int i = 0; i < num_courses_; ++i) {
      os << grades_[i] << ", ";
    }
  }
  bool operator < (const Student& str) const
  {
    return (name_ < str.name_);
  }
};


int main(int argc, char ** argv) {
  srand(time(NULL));
  if (argc == 2){
    int numbOfStudents = atoi(argv[1]);
    std::vector<Student> studentVec;
    for (int i = 0; i < numbOfStudents; i++){
      studentVec.push_back(Student());
    }

    std::sort(studentVec.begin(), studentVec.end());
    for (std::vector<Student>::iterator xi = studentVec.begin(); xi != studentVec.end(); xi++) {
      std::cout << *xi << std::endl;
    }

  }
  else{
    std::cout << "Usage: " << argv[0] << " {numb} " << std::endl;
  }
  return 0
}

当我运行排序时会出现错误(我知道它是排序的,因为如果我将其注释掉,它就会正常工作)。带有错误代码

In file included from /usr/include/c++/4.9/algorithm:62:0,
             from main.cpp:5:
/usr/include/c++/4.9/bits/stl_algo.h: In instantiation of ‘void   std::__insertion_sort(_RandomAccessIterator, _RandomAccessIterator, _Compare) [with _RandomAccessIterator = __gnu_cxx::__normal_iterator<Student*, std::vector<Student> >; _Compare = __gnu_cxx::__ops::_Iter_less_iter]’:
/usr/include/c++/4.9/bits/stl_algo.h:1884:70:   required from ‘void std::__final_insertion_sort(_RandomAccessIterator, _RandomAccessIterator, _Compare) [with _RandomAccessIterator = __gnu_cxx::__normal_iterator<Student*, std::vector<Student> >; _Compare = __gnu_cxx::__ops::_Iter_less_iter]’
/usr/include/c++/4.9/bits/stl_algo.h:1970:55:   required from ‘void std::__sort(_RandomAccessIterator, _RandomAccessIterator, _Compare) [with _RandomAccessIterator = __gnu_cxx::__normal_iterator<Student*, std::vector<Student> >; _Compare = __gnu_cxx::__ops::_Iter_less_iter]’
/usr/include/c++/4.9/bits/stl_algo.h:4685:72:   required from ‘void std::sort(_RAIter, _RAIter) [with _RAIter = __gnu_cxx::__normal_iterator<Student*, std::vector<Student> >]’
main.cpp:79:50:   required from here
/usr/include/c++/4.9/bits/stl_algo.h:1851:17: error: use of deleted function ‘Student& Student::operator=(Student&&)’
    *__first = _GLIBCXX_MOVE(__val);

我之前进行了搜索,这帮助我获得了添加“<”重载的结果,尽管我仍然收到错误。谁能帮忙指出错误在哪里吗?谢谢。 (我使用 g++ --std=c++11 编译)

最佳答案

成员变量const int num_courses_;是const,这意味着它必须在构造函数的初始值设定项列表中设置。

num_courses_ 不能由复制赋值运算符 Student& Student::operator=(Student&&) 设置,因此它会阻止编译器为该类生成复制赋值运算符。由于没有可用的复制赋值运算符,而 std::sort 函数需要它才能工作,因此编译失败并提示复制赋值运算符不可用。

只需删除 const 并将变量声明为 int num_courses_,您的代码就应该可以工作。

关于c++ - 使用自定义类的 std::sort 错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/36298778/

相关文章:

c# - 如何更正通用排序代码以对可空类型进行排序

vb.net - 根据键对字典进行排序

C++ OpenSceneGraph 更改相机眼睛高度

python - swig:扩展类模板以提供 __str__

c++ - GCC 建立链接问题

javascript - 等同于基于 Jquery Datatables 中的基础数据进行排序的 mRender

c - 以升序方式对链表进行排序

c++ - 二叉树的示例实现不适用于大量值

c++ - 使用 boost 读取复杂的 JSON 数组

java - ArrayList 或 HashMap<String, String> 的不同排序方式