#template comparator not recognized by stl function

17 messages · Page 1 of 1 (latest)

torpid light
#

Hi, I tried making a customized comparator for a struct and then do a merge sort with it. but it keeps giving this error:

/usr/include/c++/11/bits/predefined_ops.h:98:22: error: no match foroperator<’ (operand types are ‘const myStruct<int>’ and ‘myStruct<int>’)
   98 |       { return __val < *__it; }

My code is like this to merge two parts of same vector.:

template<typename T>
struct myStruct
{
    T key;
    int cnt;
    myStruct() {
        cnt = std::numeric_limits<int>::max();
    }
    bool operator <(const myStruct<T>& rec2) {
        if (key<rec2.key)
            return true;
        else
            return false;
    }
    bool operator >(const myStruct<T>& rec2) {
        if (key>rec2.key)
            return true;
        else
            return false;
    }
};
template <typename T>
struct myStruct_Comparator {
    bool operator()(const myStruct<T>& a, const myStruct<T>& b) const {
        return a.key <= b.key;
    }
};

int main()
{
    typedef int DATATYPE;
    ... ...
    std::vector<myStruct<DATATYPE>> aaa = std::vector<myStruct<DATATYPE>>(100);
    for(int i=0;i<50;i++)
    {
        aaa[i].key = dist(gen); // get random integer
        std::sort(aaa.begin(),(aaa.begin()+50));
    }
    for(int i=50;i<100;i++)
    {
        aaa[i].key = dist(gen); // get random integer
        std::sort(aaa.begin()+50,aaa.end());
    }
    std::cout<<"Unsorted: "<<std::endl;
    for(auto i: aaa) {std::cout<<"Element: "<<i.key<<std::endl;}
    //std::inplace_merge(aaa.begin(), aaa.begin()+50, aaa.end());
    std::inplace_merge(&aaa[0], &aaa[50], &aaa[100]);

    std::cout<<"\nSorted: "<<std::endl;
    for(auto i: aaa) {std::cout<<"Element: "<<i.key<<std::endl;}
}```

Any advices? Thanks  much.
plain widgetBOT
#

When your question is answered use !solved to mark the question as resolved.

Remember to ask specific questions, provide necessary details, and reduce your question to its simplest form. For tips on how to ask a good question use !howto ask.

torpid light
#

template comparator not recognized by stl function

rose dragon
#

you should make your member operator< implementations const

#

e. g. bool operator <(const myStruct<T>& rec2) const {

torpid light
rose dragon
#

it apears you already did

#

bool operator()(const myStruct<T>& a, const myStruct<T>& b) const {

#

in genreal you should always mark member functions that do not modify *this as const to indicate that they can be used on const instances

#

if you're using this as the comparator for a map or set they have to be so marked because the keys of a map or set are const

torpid light
rose dragon
#

it's generally a good idea to add const to member functions that do not modify *this so that they can be used with const instances

#

T matches an argument of const T but const T never matches an argument of T

#

that postpended const is basically changing this from T* to const T* in that member function

torpid light
rose dragon
#

the compiler won't allow you to pass a pointer or reference to const T to a function that accepts a pointer or reference to non const T because that function is not promising not to try to modify that object

rapid parrot
#

mark the member function const