C2678 二进制“<”: 没有找到接受“const ***”类型的左操作数的运算符解决办法

正确代码如下:

#include<iostream> #include<string> #include<map> using namespace std; /*仿函数的应用*/ typedef struct tagStudentinfo { int niD; string strName; }Studentinfo, *PStudentinfo; //学生信息 class sort { public: bool operator() (Studentinfo const &_A, Studentinfo const &_B) const { if (_A.niD < _B.niD) return true; if (_A.niD == _B.niD) return _A.strName.compare(_B.strName) < 0; return false; } }; void main() { map<int,Studentinfo> mapStudent; map<int,Studentinfo>::iterator mit; Studentinfo student1; student1.niD = 1; student1.strName = "cc"; mapStudent.insert(pair<int,Studentinfo>(80,student1)); Studentinfo student2; student2.niD = 1; student2.strName = "aa"; mapStudent.insert(pair<int,Studentinfo>(90,student2)); Studentinfo student3; student3.niD = 2; student3.strName = "dd"; mapStudent.insert(pair<int,Studentinfo>(100,student3)); cout << "students info:" << endl;; for (mit = mapStudent.begin(); mit != mapStudent.end(); mit++) { cout << mit->second.niD<< "," << mit->second.strName<<"," << mit->first << endl; } system("pause"); }

重写了operator()方法。

先是把Studentinfo类放在了左边,报错,将int放在左边之后编译通过。

把基本类型int,double,string,vector等放在左边,方便map进行排序。

感觉是oprator()方法重载的错误,目前还没找到更好的解决办法,之后找到了再进行补充。

唯有热爱方能抵御岁月漫长。
原文地址:https://www.cnblogs.com/syq816/p/12239080.html