zoukankan      html  css  js  c++  java
  • C++出现:error: passing 'const Employee' as 'this' argument of 'int Employee::getSalary()' discards qualifiers [-fpermissive]

    代码如下

    #include <iostream>
    #include <map>
    #include <string>
    #include <ctime>
    
    #define MAX 10
    
    using namespace std;
    
    class Employee{
    public:
        string m_Name;
        Employee(string name, int salary)
        {
            this->m_Name = name;
            this->m_Salary = salary;
        }
        void setSalary(int salary)
        {
            this->m_Salary = salary;
        }
        int getSalary()
        {
            return this->m_Salary;
        }
    private:
        int m_Salary;
    };
    class MyCompare{
    public:
        bool operator()(const Employee &p1, const Employee &p2) const
        {
            return p1.getSalary() > p2.getSalary();
        }
    };
    void printEmployee(const multimap<int, Employee, MyCompare> &mp)
    {
    
    }
    int main(void)
    {
    
        cout << rand() / RAND_MAX << endl;
    
        return 0;
    }

    运行出错

    H:4_map_employeemain.cpp:33: error: passing 'const Employee' as 'this' argument of 'int Employee::getSalary()' discards qualifiers [-fpermissive] return p1.getSalary() > p2.getSalary(); 

    分析

    提示说把'const Employee'作为参数传给'int Employee::getSalary()' 这个函数,this实参丢了限定符,仔细一看发现这个函数在定义和实现的时候并没有用const修饰,解决办法就在这个函数后加const修饰即可。

  • 相关阅读:
    chkconfig命令
    PHP中的WebService
    MySQL 中联合查询效率分析
    javascript中json对象长度
    Replace Pioneer
    c++ 调用matlab程序
    ubuntu 安装 sublime
    一些地址收藏
    学习笔记草稿
    Redis Cluster 集群使用(3)
  • 原文地址:https://www.cnblogs.com/BASE64/p/14321060.html
Copyright © 2011-2022 走看看