gpt4 book ai didi

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

转载 作者:搜寻专家 更新时间:2023-10-31 02:16:58 24 4
gpt4 key购买 nike

我查看了 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
}

当我运行 sort 时出现错误(我知道它是 sort 因为如果我注释掉它,它会正常工作)。带错误码

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/

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