samedi 11 juillet 2020

C++ Custom Comparison Functors

I am trying to pass a custom functor into std::map.

So, I declare the following functor and the class whose member is a map in a HEADER file.

class Comp {
        bool g;
        public:
                Comp(bool greater) : g(greater) {}
                bool operator()(float lhs, float rhs) const {
                        if (g) return lhs >= rhs;
                        return lhs < rhs;
                }
};

class OrderBook {
        u_char OrderBookType;
        std::map<float, std::vector<float*>, Comp> OrderBookData;

        public:
                OrderBook(u_char);
                float best_bid_ask(int);
};

And in a .cpp file, I define the constructor for OrderBook class as follows to initialize the std::map.

OrderBook::OrderBook(u_char bookType)  {
        OrderBookType = bookType;
        OrderBookData(Comp(bookType == 'B'));
}

However, when I try to compile the program, I run into a "type does not provide a call operator" error:

error: type 'std::map<float, std::vector<float *>, Comp>' does not provide a call operator
        OrderBookData(Comp(bookType == 'B'));

I am very confused as to why I am running into this error.

Any help is much appreciated.

Aucun commentaire:

Enregistrer un commentaire