I'm stuck when doing an exercise from C++ Primer 5th Edition ,which goes like
Exercise 11.11: Redefine bookstore without using decltype.
Below is the relevant codes in this book:
multiset<Sales_data, decltype(compareIsbn)*> bookstore(compareIsbn);
The code for class Sales_data is a little bit verbose to post here,so I wrote a simpler one and defined the multiset in the same style, as shown below. It compiled without any error.
class A
{
int lenth;
public:
int getLenth() const {return lenth;}
};
bool compareA(const A &a1, const A &a2)
{
return a1.getLenth() < a2.getLenth();
}
int main()
{
std::multiset<A, decltype(compareA)*> m1(compareA);
return 0;
}
I guess this exercise want readers to review knowledge of function pointers, so I tried another way to define it. But it doesn't work. Below is where I'm stuck
int main()
{
bool (*fp) (const A &a1, const A &a2);
fp = &compareA;
std::multiset<A, fp*> m1(fp);
return 0;
}
Three errors were generated:
error: template argument 2 is invalid
error: invalid type in declaration before '(' token
error: invalid conversion from 'bool (*)(const A&, const A&)' to 'int' [-fpermissive]
What's my problem?How to fix it?