简体   繁体   中英

Custom std::set comparator - No match for call to […]

I have applied the accepted answer to this question in a templated class, like this:

template <typename T, typename S>
struct sizeCompare {
    bool operator() (const std::set<Node<T, S>>& lhs, const std::set<Node<T, S>>& rhs) const {
        return lhs.size() < rhs.size() || (lhs.size() == rhs.size() && lhs < rhs);
    }
};

template <typename T, typename S>
class Graph {
    private:
        std::set<std::set<Node <T, S>>, sizeCompare <T, S>> cliques = {};
};

Now I am trying to use std::lower_bound in one of the methods of this class, and for that I want to re-use my sizeCompare structure. I have therefore followed the syntax found in this answer , like this:

    // Still in Graph.h
    std::set<Node <T, S>> getCliqueOfSize(const lli n) {
        // ...
        typename std::set<std::set<Node <T, S>>, sizeCompare <T, S>>::const_iterator it = std::lower_bound(cliques.begin(), cliques.end(), n, sizeCompare<T, S>());
        // ...
    }

However, compiling this yields the following error:

C:/PROGRA~2/CODEBL~1/mingw64/lib/gcc/x86_64-w64-mingw32/6.2.0/include/c++/bits/predefined_ops.h:144:11:
error: no match for call to
(sizeCompare<long long, void*>) (const std::set<Node<long long, void*>, std::less<Node<long long, void*> >, std::allocator<Node<long long, void*> > >&, const long long&)

  { return bool(_M_comp(*__it, __val)); }
           ^~~~~~~~~~~~~~~~~~~~~~~~~~~

The error stack indicates that this error happens when generating the method shown above. The only time where I instantiate the Graph class, I use <long long, void *> , so that is where the types shown in the compilation error come from. Can anyone explain why I am getting this error, and possibly how to fix it?

You have a comparator for two sets, but you are calling std::lower_bound() on a set with a value of key_type . That is, you are searching for a key in a set. But your comparator does not support set < long long int , only set < set .

Maybe you need to define an additional comparator:

bool operator() (const std::set<Node<T, S>>& lhs, const T& rhs) const;

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM