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()方法重載的錯誤,目前還沒找到更好的解決辦法,之后找到了再進行補充。


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM