首页 > 编程语言 > 详细

C++出现:error: passing 'const Employee' as 'this' argument of 'int Employee::getSalary()' discards qualifiers [-fpermissive]

时间:2021-01-24 18:21:56      阅读:35      评论:0      收藏:0      [点我收藏+]

代码如下

#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:\04_map_employee\main.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修饰即可。

C++出现:error: passing 'const Employee' as 'this' argument of 'int Employee::getSalary()' discards qualifiers [-fpermissive]

原文:https://www.cnblogs.com/BASE64/p/14321060.html

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!