error: passing xxx as 'this' argument of xxx discards qualifiers

#include <set>

using namespace std;

class StudentT {

public:
    int id;
    string name;
public:
    StudentT(int _id, string _name) : id(_id), name(_name) {
    }
    int getId() {
        return id;
    }
    string getName() {
        return name;
    }
};

inline bool operator< (StudentT s1, StudentT s2) {
    return  s1.getId() < s2.getId();
}

int main() {

    set<StudentT> st;
    StudentT s1(0, "Tom");
    StudentT s2(1, "Tim");
    st.insert(s1);
    st.insert(s2);
    set<StudentT> :: iterator itr;
    for (itr = st.begin(); itr != st.end(); itr++) {
        cout << itr->getId() << " " << itr->getName() << endl;
    }
    return 0;
}

In line:

cout << itr->getId() << " " << itr->getName() << endl;

It give an error that:

../main.cpp:35: error: passing 'const StudentT' as 'this' argument of 'int StudentT::getId()' discards qualifiers

../main.cpp:35: error: passing 'const StudentT' as 'this' argument of 'std::string StudentT::getName()' discards qualifiers

What's wrong with this code? Thank you!

c++

shareimprove this question

edited May 8 '12 at 11:51

Drew Noakes

177k108507595

asked May 12 '11 at 4:52

JASON

2,53542033

  • 11

    Where is line 35 in your code snippet? – In silico May 12 '11 at 4:54 

  • 65

    I wish GCC would improve this error message, e.g. "discards qualifiers" -> "breaks const correctness" – jfritz42 Oct 8 '13 at 22:06

  • 12

    @jfritz42: Would be confusing for the case it discards volatile – PlasmaHH Dec 9 '13 at 15:40

  • 1

    @PlasmaHH the error message would be split into "breaks const correctness" and "breaks volatile correctness". Now, not many people will think about something being volatile correct – Caleth Aug 31 '17 at 13:50

add a comment

4 Answers

activeoldestvotes

up vote377down voteaccepted

The objects in the std::set are stored as const StudentT. So when you try to call getId() with the const object the compiler detects a problem, mainly you're calling a non-const member function on const object which is not allowed because non-const member functions make NO PROMISE not to modify the object; so the compiler is going to make a safe assumption that getId() might attempt to modify the object but at the same time, it also notices that the object is const; so any attempt to modify the const object should be an error. Hence compiler generates an error message.

The solution is simple: make the functions const as:

int getId() const {
    return id;
}
string getName() const {
    return name;
}

This is necessary because now you can call getId() and getName() on const objects as:

void f(const StudentT & s)
{
     cout << s.getId();   //now okay, but error with your versions
     cout << s.getName(); //now okay, but error with your versions
}

As a sidenote, you should implement operator< as :

inline bool operator< (const StudentT & s1, const StudentT & s2)
{
    return  s1.getId() < s2.getId();
}

Note parameters are now const reference.

shareimprove this answer

edited May 6 at 18:40

Tomer

300315

answered May 12 '11 at 5:02

Nawaz

244k82540742

  • 2

    Such a clear explanation. Thanks. But I wonder about your last code snippet. Why use reference in the function parameter ? const StudentT & s1, const StudentT & s2 ? – Rafael Adel Jun 12 '16 at 9:44

  • @RafaelAdel: You use reference to avoid unecessary copy, and const because the function doesn't need to modify the object, so the const enforces this at compile-time. – Nawaz Jun 12 '16 at 10:41

add a comment

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值