码迷,mamicode.com
首页 > 其他好文 > 详细

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

时间:2020-01-28 23:22:04      阅读:253      评论:0      收藏:0      [点我收藏+]

标签:ios   clu   main   std   iostream   没有   ret   通过   pause   

正确代码如下:

#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()方法重载的错误,目前还没找到更好的解决办法,之后找到了再进行补充。

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

标签:ios   clu   main   std   iostream   没有   ret   通过   pause   

原文地址:https://www.cnblogs.com/syq816/p/12239080.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!