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

    唯有热爱方能抵御岁月漫长。
  • 相关阅读:
    Druid连接池的简单使用
    JDBC工具包commons-dbutils的基本介绍
    Java IO: ByteArrayOutputStream使用
    开源数据库连接池之Tomcat内置连接池
    jquery里面的$(this)和this的区别
    css自定义属性(css变量)
    CSS选择器
    IntelliJ IDEA 学习笔记
    JSP基本的语法、3个编译指令、7个动作指令、9个内置对象
    CSS3 box-shadow
  • 原文地址:https://www.cnblogs.com/syq816/p/12239080.html
Copyright © 2020-2023  润新知