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修饰即可。

  • 相关阅读:
    mysql 查询优化 ~ select count 知多少
    mongodb 案例 ~ 经典故障案例
    printk 驱动调试
    21天学通C++学习笔记(七):函数
    OPC UA
    MQTT
    分库分表
    水平、垂直权限问题(横向越权与纵向越权)
    数据库中的行转列和列转行
    面试知识点
  • 原文地址:https://www.cnblogs.com/BASE64/p/14321060.html
Copyright © 2011-2022 走看看