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

  • 相关阅读:
    白书数据结构基础总结
    UVA 10557 XYZZY 结题报告
    UVA 10047 The Monocycle 解题报告
    二叉查找树及其C语言实现
    堆排序及其c语言实现
    约瑟夫环问题小结
    KMP算法总结
    UVA 10129 Play on Words 解题报告
    深入浅出Node.js (8)
    洛谷 P1757 通天之分组背包
  • 原文地址:https://www.cnblogs.com/BASE64/p/14321060.html
Copyright © 2011-2022 走看看