lundi 6 janvier 2020

C++11 complie error when try to insert element in multiset

The code was as below

#include <set>
#include <iostream>
#include <string>
#include <memory>

using namespace std;
class Quote {
    public:
        int getnum() {
            return num;
        }
    private:
        int num;
};

class basket {
    public:
        void add_item(const shared_ptr<Quote> &b) {
            setQuo.insert(b);
        }
    private:
        static bool compare(shared_ptr<Quote> &l, shared_ptr<Quote> &r) {
            return l->getnum() < r->getnum();
        }
        multiset<shared_ptr<Quote>, decltype(compare)*> setQuo{compare};
};


int main()
{
    cout << "start" << endl;
}

I found thatsetQuo.insert(b);will lead complie error. Complie error was as below

*/usr/include/c++/7/bits/stl_tree.h:2069:51: error: binding reference of type ‘std::shared_ptr<Quote>&’ to ‘const key_type {aka const std::shared_ptr<Quote>}’ discards qualifiers
__x = _M_impl._M_key_compare(__k, _S_key(__x)) ?*

*/usr/include/c++/7/bits/stl_tree.h:1750:10: error: binding reference of type ‘std::shared_ptr<Quote>&’ to ‘const std::shared_ptr<Quote>’ discards qualifiers*

The code was looks right for me, this question is really confused me.

Aucun commentaire:

Enregistrer un commentaire