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.
You need to use initializer list syntax to construct the members:
OrderBook::OrderBook(u_char bookType) :
OrderBookType{bookType},
OrderBookData{Comp{bookType == 'B'}} { }
At the point where you are trying to construct the map, it is already default-constructed. The syntax you're using looks like a function call, so the compiler looks for a suitable operator()
function on the map, but doesn't find it -- hence the error.