gpt4 book ai didi

c++ - Algorithm文件解决方案中的错误c2784、c2780和c2676?

转载 作者:塔克拉玛干 更新时间:2023-11-03 07:16:48 26 4
gpt4 key购买 nike

我目前正在为我的编程 II 类(class)编写一个程序,目前一切看起来都不错,但是当我尝试构建我的程序时。我在错误列表中收到以下消息:

Error   1   error C2784: ''unknown-type' std::operator -(std::move_iterator<_RanIt> &,const std::move_iterator<_RanIt2> &)' : could not deduce template argument for 'std::move_iterator<_RanIt> &' from 'std::_List_iterator<_Mylist>' c:\program files (x86)\microsoft visual studio 11.0\vc\include\algorithm    3868    1   ConsoleApplication46
Error 2 error C2784: ''unknown-type' std::operator -(const std::reverse_iterator<_RanIt> &,const std::reverse_iterator<_RanIt2> &)' : could not deduce template argument for 'const std::reverse_iterator<_RanIt> &' from 'std::_List_iterator<_Mylist>' c:\program files (x86)\microsoft visual studio 11.0\vc\include\algorithm 3868 1 ConsoleApplication46
Error 3 error C2784: ''unknown-type' std::operator -(const std::_Revranit<_RanIt,_Base> &,const std::_Revranit<_RanIt2,_Base2> &)' : could not deduce template argument for 'const std::_Revranit<_RanIt,_Base> &' from 'std::_List_iterator<_Mylist>' c:\program files (x86)\microsoft visual studio 11.0\vc\include\algorithm 3868 1 ConsoleApplication46
Error 5 error C2780: 'void std::_Sort(_RanIt,_RanIt,_Diff)' : expects 3 arguments - 4 provided c:\program files (x86)\microsoft visual studio 11.0\vc\include\algorithm 3868 1 ConsoleApplication46
Error 4 error C2676: binary '-' : 'std::_List_iterator<_Mylist>' does not define this operator or a conversion to a type acceptable to the predefined operator c:\program files (x86)\microsoft visual studio 11.0\vc\include\algorithm 3868 1 ConsoleApplication46

这是我的程序源代码:

// ConsoleApplication42.cpp : Defines the entry point for the console application.
//

#include "stdafx.h"
#include <iostream>
#include <list>
#include <cstdlib>
#include <time.h>
#include <algorithm>
#include <string>

using namespace std;

const int numberOfStudents = 9;
string names[numberOfStudents] = {"Abe","Billy","Carl","Dillan","Eddie","Felix","Gill","Herald","Isaac"};

struct StudentInfo {
string name;
int grade;



bool operator< (int grade){
return grade < grade;
}

bool operator< (string name){
return name < name;
}
};



void populateStudentRecords(list<StudentInfo>Students,list<int>::iterator iter, int x){

for(auto iter = Students.begin(); iter != Students.end(); ++iter){
iter->name = names[x];
iter->grade = x++;
x = x++;

}



}

bool sortByName(const StudentInfo &x, const StudentInfo &y){
return x.name < y.name;
}

bool sortByGrade(const StudentInfo &x, const StudentInfo &y){
return x.grade < y.grade;
}

void displayRecords(list<StudentInfo>Records, list<int>::iterator iter){
for(auto iter = Records.begin(); iter != Records.end(); ++iter){
cout<<iter->name<<" - "<<iter->grade<<endl;
}
}

void displayMaxAndMinGrade(list<StudentInfo>Records,list<int>::iterator iter){
for(auto iter = Records.begin(); iter != Records.end(); ++iter){
cout<<iter->name<<" - " <<iter->grade<<endl;
iter = Records.end();
cout<<iter->name<<" - " <<iter->grade<<endl;
}
}

int _tmain(int argc, _TCHAR* argv[])
{
list<StudentInfo>Records (numberOfStudents);
list<int>::iterator iter;

populateStudentRecords(Records,iter,0);
sort(Records.begin(),Records.end(),sortByName);
displayRecords(Records,iter);
sort(Records.begin(),Records.end(),sortByGrade);
cout<<" "<<endl;
displayMaxAndMinGrade(Records,iter);

return 0;
}

我已尝试将构造函数添加到我的数据结构并将代码粘贴到新项目中。任何帮助将不胜感激。

最佳答案

如评论中所述,标准库 sort 函数需要随机访问迭代器。您遇到的错误很可能是因为 list 不使用随机访问迭代器,因此不提供 operator-

可能最直接的解决方案是为您的容器使用 vector 而不是 list

关于c++ - Algorithm文件解决方案中的错误c2784、c2780和c2676?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/32554657/

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